Uploading more than one image - spring

Dear All,
Working on Spring MVC. I want to upload more than one images from the client. How to achieve it. I know how to handle the multipart form data for single image. But now I am expecting some data with some images from the client.
Any help or url that will help me.
Thanks,
Op

Image is also a file. Whether you would be storing it in database / in file system but it is still a file.
In spring MVC, you could do as shown in the below link:
http://viralpatel.net/blogs/spring-mvc-multiple-file-upload-example/

Here are the code i tried and it is working fine at my end.
//Handle multiple images
#RequestMapping(method = RequestMethod.POST, value="upload", consumes=MediaType.MULTIPART_FORM_DATA_VALUE,
produces=MediaType.APPLICATION_JSON_VALUE)
public #ResponseBody JSONResponse uploadImages(HttpServletRequest req)
throws Exception {
try{
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req;
Set set = multipartRequest.getFileMap().entrySet();
Iterator i = set.iterator();
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
String fileName = (String)me.getKey()+"_"+System.currentTimeMillis();
MultipartFile multipartFile = (MultipartFile)me.getValue();
System.out.println("Original fileName - " + multipartFile.getOriginalFilename());
System.out.println("fileName - " + fileName);
saveImage(fileName, multipartFile);
}
}
catch(Exception e){
e.printStackTrace();
}
return new JSONResponse();
}

Related

corrupted PDF file downloaded from spring rest api

I am developing a rest end point to download a existing pdf file. I'm able to download PDF with size, but when I open, getting error as
'Adobe reader couldnot open it is either not supported file type or
because file has been damaged'.
I have noticed that in postman, response header content type is application/pdf;charset=UTF-8. I'm not sure if this is the cause.
With simple spring boot application I'm able to download, but in our project when I implement the same code, it is not working.
#GetMapping( path= "/s3/downloads")
public ResponseEntity<byte[]> downloadFile()
{
InputStream in = getClass().getResourceAsStream("/com/consulting/cloud/filetransfers/rest/template_attendance_en_green_full.pdf");
byte[] generatedCertificate = null;
String downloadFileName = "pdfFile1.pdf";
ResponseEntity<byte[]> responseEntity = ResponseEntity.status( HttpStatus.NO_CONTENT ).body( generatedCertificate );
try {
generatedCertificate = StreamUtils.copyToByteArray(in);
HttpHeaders headers = new HttpHeaders();
headers.add("Access-Control-Allow-Origin", "*");
headers.setContentType( MediaType.APPLICATION_PDF );
headers.setContentLength( generatedCertificate.length );
headers.setContentDispositionFormData( "attachment", downloadFileName );
responseEntity = ResponseEntity.ok().headers( headers ).body( generatedCertificate );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return responseEntity;
}
The file is opened as expected.

How to mock a multipart file upload when using Spring and Apache File Upload

The project I'm working on needs to support large file uploads and know the time taken during their upload.
To handle the large files I'm using the streaming API of Apache FileUpload, this also allows me to measure the time taken for the complete stream to be saved.
The problem I'm having is that I cannot seem to be able to utilise MockMvc in an Integration Test on this controller. I know that the controller works as I've successfully uploaded files using postman.
Simplified Controller Code:
#PostMapping("/upload")
public String handleUpload(HttpServletRequest request) throws Exception {
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iterStream = upload.getItemIterator(request);
while (iterStream.hasNext()) {
FileItemStream item = iterStream.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
if (!item.isFormField()) {
// Process the InputStream
} else {
String formFieldValue = Streams.asString(stream);
}
}
}
Simplified Test Code:
private fun uploadFile(tfr: TestFileContainer) {
val mockFile = MockMultipartFile("file", tfr.getData()) // .getData*() returns a ByteArray
val receiveFileRequest = MockMvcRequestBuilders.multipart("/upload")
.file(mockFile)
.contentType(MediaType.MULTIPART_FORM_DATA)
val result = mockMvc.perform(receiveFileRequest)
.andExpect(status().isCreated)
.andExpect(header().exists(LOCATION))
.andReturn(
}
This is the error I'm currently getting
org.apache.tomcat.util.http.fileupload.FileUploadException: the
request was rejected because no multipart boundary was found
Can anyone help?
The MockMultipartFile approach won't work as Spring does work behind the scenes and simply passes the file around.
Ended up using RestTemplate instead as it actually constructs requests.

Implement Antibot Captcha with Java Spring MVC using Thymeleaf and JQuery AJAX

I'm trying to implement Captcha with my Spring MVC application. Although there are some examples of how to do that, I couldn't find any where the form is submitted with JQuery AJAX and the template engine is Thymeleaf.
My main source of information is this.
I've added the dependencies and the servlet, but I'm struggling with the template part (where I'm using Thymeleaf and JQuery AJAX to send my form to the controller) and to do the validation in the controller.
I don't necessarily want to use Captcha as my antibot framework, so if you have any ideas using other framework, I would be more than glad to hear them.
Ok, so I ended up using Cage Captcha generator. It can be integrated with Maven and it's fairly easy to be implemented with a Spring MVC application with JQuery AJAX.
/**
* Generates captcha as image and returns the image path
* stores the captcha code in the http session
* and deletes older, unused captcha images.
*/
#RequestMapping(value = "/captcha/generate", method = RequestMethod.GET, produces="application/json")
#ResponseBody
public ResponseEntity<CaptchaRequestData> generateCaptcha(HttpSession session) {
String captchaImageUploadDirectory = environment.getProperty("captcha_image_folder");
String captchaWebAlias = environment.getProperty("captcha_web_alias");
//Creating dir or making new one if it doesn't exist
File file = new File(captchaImageUploadDirectory);
if (!file.exists()) {
try {
file.mkdirs();
} catch(Exception e){}
}
String timeSuffix = DBUtils.getDateTimeAsString();
String fileName = CAPTCHA_IMAGE_PREFIX + timeSuffix + "." + CAPTCHA_IMAGE_EXTENSION;
String fullFilename = captchaImageUploadDirectory + fileName;
//Generating the captcha code and setting max length to 4 symbols
Cage currGcage = new YCage();
String captchaToken = currGcage.getTokenGenerator().next();
if (captchaToken.length() > CAPTCHA_CODE_MAX_LENGTH) {
captchaToken = captchaToken.substring(0, CAPTCHA_CODE_MAX_LENGTH).toUpperCase();
}
//Setting the captcha token in http session
session.setAttribute("captchaToken", captchaToken);
try {
OutputStream os = new FileOutputStream(fullFilename, false);
currGcage.draw(captchaToken, os);
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
deleteFilesOlderThan(captchaImageUploadDirectory, CAPTCHA_IMAGE_LIFE_MILLISECONDS, CAPTCHA_IMAGE_EXTENSION);
CaptchaRequestData data = new CaptchaRequestData(captchaWebAlias + fileName);
return new ResponseEntity<>(data, HttpStatus.OK);
}
Then when I'm creating the object I check if the given code equals the one, stored in the session:
if (!httpSession.getAttribute("captchaToken").equals(bindingData.getCaptchaCode())) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
Finally if the provided captcha is incorrect I generate a new one.

loading a pdf in-browser from a file in the server file system?

How can I get a pdf located in a file in a server's directory structure to load in a browser for users of a Spring MVC application?
I have googled this and found postings about how to generate PDFs, but their answers do not work in this situation. For example, this other posting is not relevant because res.setContentType("application/pdf"); in my code below does not solve the problem. Also, this other posting describes how to do it from a database but does not show full working controller code. Other postings had similar problems that caused them not to work in this case.
I need to simply serve up a file (not from a database) and have it been viewable by a user in their browser. The best I have come up with is the code below, which asks the user to download the PDF or to view it in a separate application outside the browser. What specific changes can I make to the specific code below so that the user automatically sees the PDF content inside their browser when they click on the link instead of being prompted to download it?
#RequestMapping(value = "/test-pdf")
public void generatePdf(HttpServletRequest req,HttpServletResponse res){
res.setContentType("application/pdf");
res.setHeader("Content-Disposition", "attachment;filename=report.pdf");
ServletOutputStream outStream=null;
try {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(new File("/path/to", "nameOfThe.pdf")));
/*ServletOutputStream*/ outStream = res.getOutputStream();
//to make it easier to change to 8 or 16 KBs
int FILE_CHUNK_SIZE = 1024 * 4;
byte[] chunk = new byte[FILE_CHUNK_SIZE];
int bytesRead = 0;
while ((bytesRead = bis.read(chunk)) != -1) {outStream.write(chunk, 0, bytesRead);}
bis.close();
outStream.flush();
outStream.close();
}
catch (Exception e) {e.printStackTrace();}
}
Change
res.setHeader("Content-Disposition", "attachment;filename=report.pdf");
To
res.setHeader("Content-Disposition", "inline;filename=report.pdf");
You should also set the Content Length
FileCopyUtils is handy:
#Controller
public class FileController {
#RequestMapping("/report")
void getFile(HttpServletResponse response) throws IOException {
String fileName = "report.pdf";
String path = "/path/to/" + fileName;
File file = new File(path);
FileInputStream inputStream = new FileInputStream(file);
response.setContentType("application/pdf");
response.setContentLength((int) file.length());
response.setHeader("Content-Disposition", "inline;filename=\"" + fileName + "\"");
FileCopyUtils.copy(inputStream, response.getOutputStream());
}
}

Spring MVC outputStream

my problem is that I have a program which does make a file & stream it out to the user & I have two problem:
1) first how do change the file name & type (x.sql)
2) what if there's an exception while making the file & I want to redirect the user to an error page because my return type of my method is void when we want to write into outputstream in servlet !
my program is like this:
#RequestMapping(value = "/test/{jobid}")
public void dumpData(#PathVariable("jobid") long jobid, HttpServletResponse response) {
try {
response.setContentType("application/octet-stream");
ServletOutputStream out = response.getOutputStream();
String downloadOutput = "";
// AM I ABLE TO SET FILENAME SO THE USER DOWNLOAD THE FILE WITH THAT NAME ?
// DOES SETHEADER HELP ME IN THIS CASE ?
... (making downloadOutput String by values coming from somewhere)
out.write(downloadOutput.getBytes("UTF-8"));
out.flush();
out.close();
} catch(SomeException e){
//WHEN THE RETURN TYPE IS VOID HOW TO REDIRECT USER TO ERROR PAGE IN CASE OF SOME PROBLEM ?
//(INFO:)IF WE DEFINE STRING AS RETURN TYPE THE PROGRAM WILL GET EXCEPTION
}
Answer to your first question:
response.setHeader( "Content-Disposition", "attachment;filename=" + filename );
//for zip file
response.setContentType("application/zip");
Here is the list of mime type for various content types.
I found a solution here. You can change return type to string and return error view name in case of error otherwise return null. What I understood from the link is that returning null and return type void is similar.
What stops you from using response.sendRedirect(errorPage)?

Resources