I want to set the data inside a cell to be a link :
public class TestExcelExport extends AbstractXlsxView {
#Override
protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setHeader("Content-Disposition", "attachment; filename=\"export.xlsx\"");
Sheet sheet = workbook.createSheet("test");
Row row_1 = sheet.createRow(1);
row_1.createCell(0).setCellValue("some data");
}
}
How to make the cell data to be a link pointing to a file in the system directory ?
CreationHelper createHelper = workbook.getCreationHelper();
Cell cell = row_1.createCell((short)0);
cell.setCellValue("System File Link");
link = createHelper.createHyperlink(Hyperlink.LINK_FILE);
link.setAddress("/usr/somedir/somefile.someext");
cell.setHyperlink(link);
This would do.
Related
My Controller use " org.apache.commons.fileupload " realized the file UPload.
see it:
#PostMapping("/upload")
public String upload2(HttpServletRequest request) throws Exception {
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter = upload.getItemIterator(request);
boolean uploaded = false;
while (iter.hasNext() && !uploaded) {
FileItemStream item = iter.next();
if (item.isFormField()) {
item.openStream().close();
} else {
String fieldName = item.getFieldName();
if (!"file".equals(fieldName)) {
item.openStream().close();
} else {
InputStream stream = item.openStream();
// dosomething here.
uploaded = true;
}
}
}
if (uploaded) {
return "ok";
} else {
throw new BaseResponseException(HttpStatus.BAD_REQUEST, "400", "no file field or data file is empty.");
}
}
and my MockMvc code is
public void upload() throws Exception {
File file = new File("/Users/jianxiaowen/Documents/a.txt");
MockMultipartFile multipartFile = new MockMultipartFile("file", new FileInputStream(file));
HashMap<String, String> contentTypeParams = new HashMap<String, String>();
contentTypeParams.put("boundary", "----WebKitFormBoundaryaDEFKSFMY18ehkjt");
MediaType mediaType = new MediaType("multipart", "form-data", contentTypeParams);
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post(baseUrl+"/upload")
.content(multipartFile.getBytes())
.contentType(mediaType)
.header(Origin,OriginValue)
.cookie(cookie))
.andReturn();
logResult(mvcResult);
}
my controller is right , it has successed in my web project,
but I want to test it use MvcMock, it has some mistake, see :
can someOne can help me?
"status":"400","msg":"no file field or data file is empty.","data":null
I don't know why it says my file is empty.
my English is poor, thank you very much if someone can help me.
The MockMvc can be used for integration testing for controllers using Apache Commons Fileupload too!
Import the org.apache.httpcomponents:httpmime into your pom.xml or gradle.properties
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.13</version>
</dependency>
Update the code to use MultipartEntityBuilder to build the multipart request on the client, and then serialize the entity into bytes, which is then set in the request content
public void upload() throws Exception {
File file = new File("/Users/jianxiaowen/Documents/a.txt");
String boundary = "----WebKitFormBoundaryaDEFKSFMY18ehkjt";
// create 'Content-Type' header for multipart along with boundary
HashMap<String, String> contentTypeParams = new HashMap<String, String>();
contentTypeParams.put("boundary", boundary); // set boundary in the header
MediaType mediaType = new MediaType("multipart", "form-data", contentTypeParams);
// create a multipart entity builder, and add parts (file/form data)
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
HttpEntity multipartEntity = MultipartEntityBuilder.create()
.addPart("file", new FileBody(file, ContentType.create("text/plain"), file.getName())) // add file
// .addTextBody("param1", "value1") // optionally add form data
.setBoundary(boundary) // set boundary to be used
.build();
multipartEntity.writeTo(outputStream); // or getContent() to get content stream
byte[] content = outputStream.toByteArray(); // serialize the content to bytes
MvcResult mvcResult = mockMvc.perform(
MockMvcRequestBuilders.post(baseUrl + "/upload")
.contentType(mediaType)
.content(content) // finally set the content
.header(Origin,OriginValue)
.cookie(cookie)
).andReturn();
logResult(mvcResult);
}
Can you try the below?
mockMvc.perform(
MockMvcRequestBuilders.multipart(baseUrl+"/upload")
.file(multiPartFile)
).andReturn();
Update:
You need to update the controller to handle the MultipartFile:
#PostMapping("/upload")
public String upload2(#RequestParam(name="nameOfRequestParamWhichContainsFileData")
MultipartFile uploadedFile, HttpServletRequest request) throws Exception {
//the uploaded file gets copied to uploadedFile object.
}
You need not use another library for managing file uploads. You can use the file upload capabilities provided by Spring MVC.
Question: How to fix displaying the same image for item of each ?
thymleaf.html
<div th:each="goals : ${goals}">
<div th:text="${goals.getId()}"></div>
<img th:src="#{'image/'+ ${goals.getId()}}" width="100px;"
height="100px;"/>
<div th:text="${goals.title}"></div>
controller of image
#GetMapping(value = "/image/{id}")
public void showProductImage(#PathVariable String id,
HttpServletResponse response) throws IOException{
Goals goals = goalsRepository.findById(UUID.fromString(id));
logger.info("I got id--"+id);
response.setContentType("image/jpeg, image/jpg, image/png,
image/gif");
response.getOutputStream().write(goals.getImage());
response.getOutputStream().close();
controller of view page
#GetMapping(value = "/goals")
public String read(Model model) {
model.addAttribute("login", new LogIn()); //it for bottom menu
model.addAttribute("currentlyPage","goals"); // it for top menu
model.addAttribute("addNewGoal", new addNewGoal()); //another page
//get all goals
List<Goals> goals = new ArrayList<>();
goalsRepository.findAll().forEach(goals::add);
model.addAttribute("goals",goals);
return "goals";
log4j
I got id--bc8c9820-9500-
I got id--5ba1d0d0-9504-
I got id--bff1d8a0-94ff-
I got id--1f0da4f0-94ff-11e9-970f-0bfed788288d
I got id--d76dd9b0-9500-11e9-81ae-c9c1d3b67a0f
I got id--4aaaac80-9512-11e9-b98b-7deb4e16d250
I got id--608dae20-94ff-11e9-89b4-83a8b92aa5c9
I expect different images, but everywhere same images.enter image description here
I found the solution.
I changed image type of entity to ByteBuffer
I changed controller by next code:
#GetMapping(value = "/image/{id:.+}")
public #ResponseBody ResponseEntity<byte[]> showProductImage(#PathVariable String id, HttpServletResponse response) throws IOException {
Goals goals = goalsRepository.findById(UUID.fromString(id));
ByteBuffer buffer =goals.getImage();
byte[] bytes = buffer.array();
return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(bytes);
}
I changed some valueOf type for converting .
When I click on any link the content should be downloaded
But this is what I get.
MastercourseController.java
#RequestMapping(value = { ControllerUriConstant.download_file }, method = RequestMethod.GET)
#ResponseBody
public void downloadingAFileById(#RequestParam("id") String id, Model model, HttpServletRequest request)
throws TechnoShineException, IOException {
String filePath = "D:/dev/testFIle.txt";
long download = Long.parseLong(id);
byte[] b = masterCourseFileFormService.getAllDownloadable(download);
OutputStream outputStream = new FileOutputStream(filePath);
outputStream.write(b);
outputStream.close();
}
MasterCourseService
public byte[] getAllDownloadable(long id) throws TechnoShineException
{
return masterCourseFormUploadDao.getAllDownloadableFiles(id);
}
MasterCourseDao
public byte[] getAllDownloadableFiles(long id) throws TechnoShineException
{
return masterCourseFormUploadMapper.getAllDownloadable(id);
}
MasterCourseMapper
public byte[] getAllDownloadable(long id) throws TechnoShineException;
You are writing the data returned by getAllDownloadable(..) to a hard-coded file. Are you sure that is what you want? I think you want to write the content returned by getAllDownloadable(..) to be written into the response. That can be done by adding a method parameter of the type HttpServletResponse to your mapping and writing into the output stream returned by HttpServletResponse#getOutputStream() and flushing (not closing!) that stream at the end.
Furthermore you have to remove the #ResponseBody annotation as this is meant to be used if the value that is returned by the mapping method returns the data that should directly be sent to the client (i.e. when sending a JSON data object or a string) without passing it to the template engine. As you are not returning anything you can remove this annotation.
Furthermore you have to set the content type of your response by invoking HttpServletResponse#setContentType(contentType: String).
In your case, the invocation would be the following:
response.setContentType("text/plain");
You complete method would look like this:
#RequestMapping(
value = ControllerUriConstant.download_file,
method = RequestMethod.GET
)
public void downloadingAFileById(#RequestParam("id") String id, HttpServletResponse response)
throws TechnoShineException, IOException {
long download = Long.parseLong(id);
byte[] b = masterCourseFileFormService.getAllDownloadable(download);
response.getOutputStream().write(b);
response.getOutputStream().flush();
}
I have experience with GWT/GAE MySQL cloud. But I don’t with BlobStore. I have to add to my project photos. To understand BlobStore I found an example: https://github.com/ikai/gwt-gae-image-gallery. But it doesn’t work. I have 2 problems:
1.Why the servlet “UploadServlet” return null as a blob key:
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
// Take uploaded image
Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(req);
System.out.println("UPL.SERV.key size=" + blobs.size());
List<BlobKey> blobKey = blobs.get("image");
ImagesService imagesService = ImagesServiceFactory.getImagesService();
String imageUrl = imagesService.getServingUrl(ServingUrlOptions.Builder.withBlobKey(blobKey.get(0))).trim();
// Uploaded image object: key, servingUrl,createdAt, ownerId
Entity uploadedImage = new Entity("UploadedImage");
uploadedImage.setProperty("blobKey", blobKey.get(0).getKeyString().trim());
uploadedImage.setProperty(UploadedImage.CREATED_AT, new Date());
uploadedImage.setProperty(UploadedImage.OWNER_ID, "anna");
// Highly unlikely we'll ever search on this property
uploadedImage.setUnindexedProperty(UploadedImage.SERVING_URL, imageUrl);
System.out.println("UPL.SERV-5- Z datastore key=" + keyString);
res.sendRedirect("/upload?uploadedImageKey=" + keyString);
}
doPost redirect :
UPL.SERV-5- key=aglub19hcHBfaWRyGgsSDVVwbG9hZGVkSW1hZ2UYgICAgICA4ggM
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("UPL.SERV-6- req="+req.getParameter("uploadedImageKey"));
String uploadedImageKey = req.getParameter("uploadedImageKey").trim();
resp.setHeader("Content-Type", "text/html");
// This is a bit hacky, but it'll work. We'll use this key in an Async
// service to
// fetch the image and image information
System.out.println("UPL.SERV-7- resp="+uploadedImageKey);
resp.getWriter().println(uploadedImageKey);
}
doGet gets :
UPL.SERV-6- req=aglub19hcHBfaWRyGgsSDVVwbG9hZGVkSW1hZ2UYgICAgICA4ggM
And send answer to “UploadPhoto”:
UPL.SERV-7- resp=aglub19hcHBfaWRyGgsSDVVwbG9hZGVkSW1hZ2UYgICAgICA4ggM
“UploadPhoto gets null.
Why I can’t to display my photo?
In datastore exists:
UPL.SERV-3- SET for datastore
UPL.SERV-3-blobkey=7cvGUXW_q9Q9QTEArWv3LA
UPL.SERV-3-key=UploadedImage(no-id-yet)
UPL.SERV-3-owner=anna
UPL.SERV-3-url=http://0.0.0.0:8888/_ah/img/7cvGUXW_q9Q9QTEArWv3LA
UPL.SERV-4- put to datastore
When the widget with is created “ImageOverlay” I get:
7cvGUXW_q9Q9QTEArWv3LA=s200:1 GET http://0.0.0.0:8888/_ah/img/7cvGUXW_q9Q9QTEArWv3LA=s200 net::ERR_ADDRESS_INVALID
It doesn't work because doGet in development mode return null.
I'm following with mkyong Jexcel with Spring tutorial and everything looks fine.it can create excel file and write sheet except one thing is I can't change my excel file name? It will display file name same as my link to controller.
Here is example
<li><h3>Jexcel Showcase</h3></li>
It will always create excel file name "report.html.xls". Anyone know how to change file name??
This is my controller
#RequestMapping(method=RequestMethod.POST)
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
//dummy data
revenueData.put("Jan-2010", "$100,000,000");
revenueData.put("Feb-2010", "$110,000,000");
revenueData.put("Mar-2010", "$130,000,000");
revenueData.put("Apr-2010", "$140,000,000");
revenueData.put("May-2010", "$200,000,000");
return new ModelAndView("jexcelSuccess","revenueData",revenueData);
}
and buildExcelDocument method
protected void buildExcelDocument(Map model, WritableWorkbook workbook,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Map<String,String> revenueData = (Map<String,String>) model.get("revenueData");
WritableSheet sheet = workbook.createSheet("Revenue Report", 0);
WritableSheet sheet2 = workbook.createSheet("Test Report", 1);
WritableSheet sheet3 = workbook.createSheet("Hello Report", 2);
sheet.addCell(new Label(0, 0, "Month"));
sheet.addCell(new Label(1, 0, "Revenue"));
int rowNum = 1;
for (Map.Entry<String, String> entry : revenueData.entrySet()) {
//create the row data
sheet.addCell(new Label(0, rowNum, entry.getKey()));
sheet.addCell(new Label(1, rowNum, entry.getValue()));
rowNum++;
}
}
Thank in advance,
Mart
Add this line to the controller:
response.setHeader("Content-Disposition", "attachment; filename=\"whatEver.xls\"");