0

Hey guys¡ I have one several problem. Perhaps it could be solver over there, but I can't find the solution.

I'm saving in request session the file that the user upload to the server. User's can upload .pdf, .doc, .xls and image extensions.

Until now, I only can return images, but no pdf or doc files. The problem appears when in my download controller, I have to set the headers for the file and it depends on the file I get from the session. I dont know how especific that. Its the code:

 @RequestMapping(value = "/api/previsualizarFacturaOriginal/{idFacturaOriginal}", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
public static ResponseEntity<byte[]> getImageFacturaOriginal(
        final @PathVariable("idFacturaOriginal") String idFacturaOriginal,
        final HttpServletRequest request) {
    try {
        //get object from the session
        final byte[] file = (byte[]) request.getSession().getAttribute(
                Naming.SESSION_PDF_FACTURA_ORIGINAL + idFacturaOriginal);

        final HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_JPEG);
        return new ResponseEntity<byte[]>(file, headers, HttpStatus.OK);
    } catch (final Exception e) {
        return new ResponseEntity<byte[]>(HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
Toni Rodriguez
  • 119
  • 2
  • 3
  • 11

1 Answers1

1

A couple of notes here:

  1. Be careful when you store large blobs in the session. I don't know the specific context of your application so take this warning with a grain of salt but if it's a public facing application a large number of uploads could quickly overwhelm your available JVM memory (unless you have carefully configured your app to store the session data to disk and the uploads sizes are small). Just food for thought. https://www.owasp.org/index.php/Unrestricted_File_Upload Another caveat to using sessions is that you now will need to worry about session stickiness or session replication if you want to have multiple application servers. Although dated you can read about other mechanisms here: What is the best place for storing uploaded images, SQL database or disk file system?

  2. I am assuming that you used Spring's MultipartFile upload which allows you to get the content type of the file pretty easily: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/multipart/MultipartFile.html. If that's the case then when the file is uploaded and stored in the session you could literally put the whole MultiPartFile in the session OR store the content type separately in the same session and just retrieve it at the time of download.

Community
  • 1
  • 1
maxjar10
  • 216
  • 1
  • 4
  • thanks Maxjar, I solved my problem partially, becasue I get the mimeType of the file, and i put it on the headers. But my app dont recognize .doc and .xls files. It seems that the template not recognizes the extension because dont call the server controller in this cases. Whats the problem whit this files? – Toni Rodriguez Sep 11 '15 at 07:53
  • I'm having a bit of difficulty understanding "the template not recognizes the extension" and "my app dont recognize .doc and .xls files". Are you referring to your client side application? That sounds like it may be a different question and you might consider starting a new question thread instead and closing this one. – maxjar10 Sep 11 '15 at 16:26