0

I know this is a many time asked question. I have went through all of the answers but cannot find a working solution. I am trying to download a pdf file, and the code block in jsp is as below

but the the data is flushed into browser window like below

%PDF-1.4 %áéëÓ 2 0 obj <> endobj 3 0 obj <> /XObject <> /Font <> >> /MediaBox [0 0 612 792] /Annots [<> >> <> >> <> >> <> >> <> >>] /Contents 16 0 R >> endobj 16 0 obj <> stream xœµ]ÛŽ$9}Ÿ¯¨ ׎ðUBH,Ëð ÞB,h—ÿ—HgUwF

please help me on this.

<%

java.io.File file=new java.io.File("C:\\down\\personal\\Card.pdf");
java.io.InputStream in = new java.io.BufferedInputStream(new java.io.FileInputStream(file));

response.setHeader("Content-Disposition", "attachment; filename=empfile.pdf");
response.setContentType("application/pdf");

org.apache.commons.io.IOUtils.copy(in, response.getOutputStream());
response.flushBuffer();

%>

also am getting below error

12:57:32,068 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/business].[spring]] (http-/127.0.0.1:8080-7) JBWEB000236: Servlet.service() for servle spring threw exception: java.lang.IllegalStateException: JBWEB000028: getOutputStream() has already been called for this response

blathur
  • 187
  • 1
  • 8
  • You cannot write headers, when something has already been written to the response (or at least when the reponse buffer has been flushed) - Probably you've printed something to the response before your `<% [...] %>` - use a servlet instead or make sure notheing (not even whitespace) has been written before. – Alexander Mar 19 '15 at 08:19
  • Is this part of a Spring Web Application? – Ralph Mar 19 '15 at 08:19
  • @Ralph Yes this is part of of spring web application.I have tried to do this in controller , but not working(even the kind of output got as mentioned in above post is not obtained .i dont know why) – blathur Mar 19 '15 at 09:05
  • @Alexander , This is the only code implementation in jsp, now.How can I ensure nothing is written in response before?. – blathur Mar 19 '15 at 09:06
  • Remove **all whitespace** (line-breaks, spaces etc.) that are **outside** of your scriptlet code. But it's not recommended to do it this way - use a servlet instead of a JSP, please. Show me the JSP and I can tell you how to change it. – Alexander Mar 19 '15 at 09:45
  • @Alexander, Not working. I have tried to implement this in controller (almost all the ways available in web).But when tried to do so, when the action is done nothing is happening. – blathur Mar 19 '15 at 10:46
  • @rahulk But that's the way how you should do it. If you run into problems, post them here – Alexander Mar 19 '15 at 12:05
  • When you use Spring (or any other MVC Framework) then you should not write JSP pages with code! -- have a look at this question for examples about how to send PDF files with Spring: http://stackoverflow.com/questions/5673260/downloading-a-file-from-spring-controllers – Ralph Mar 20 '15 at 06:58
  • @Ralph, I think I have tried those all – blathur Mar 23 '15 at 12:20
  • @Alexander, am posting the methods I have tried in the answer block.Kindly have a look – blathur Mar 23 '15 at 12:20
  • rahul k: then do not post this **** jsp "solutuin", post and use the controller based version instead – Ralph Mar 23 '15 at 15:11
  • @Ralph, As you said I have tried to implement this in controller, I was saying that I have tried all those ways in the post you referred(please check the post below) . But even that binary output was not available. I am doubting whether this is something related jboss 7 server or the some filters in between playing the game. – blathur Mar 24 '15 at 04:54
  • @rahul k: To say it as clear as I can: when you have implemented the file handling in an controller, then post the controller, but not the JSP way (because it is, lets say it nice, not the recommended way to implement logic). – Ralph Mar 24 '15 at 07:19
  • @Ralph, I have tried the both. JSP way was atleast producing some results, but not the controller implementation.As you suggested I am now trying to implement it in controller. I have posted the controller method in answer section(I thought that will be convenient to read). Am not getting any results there.Kindly help. – blathur Mar 24 '15 at 09:06

1 Answers1

0

This is what I have tried

@RequestMapping(value = {"/create","/edit"}, params="Action=go1",produces=MediaType.APPLICATION_OCTET_STREAM_VALUE) @ResponseBody public FileSystemResource getFile(HttpServletRequest request,HttpServletResponse response,ModelMap modelMap) { java.io.File file=new java.io.File("C:\RAHUL\personal\HDFC Bank Credit Card.pdf"); return new FileSystemResource(file); }

@RequestMapping(value = {"/create","/edit"}, params = "Action=go2")
public ResponseEntity<InputStreamResource> downloadStuff(HttpServletRequest request)
                                                                  throws IOException {
    java.io.File file=new java.io.File("C:\\RAHUL\\personal\\HDFC Bank Credit Card.pdf");
    HttpHeaders respHeaders = new HttpHeaders();

    respHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    respHeaders.setContentLength(12345678);
    respHeaders.setContentDispositionFormData("attachment", "fileNameIwant.pdf");

    InputStreamResource isr = new InputStreamResource(new FileInputStream(file));
    return new ResponseEntity<InputStreamResource>(isr, respHeaders, HttpStatus.OK);
}

@RequestMapping(value = {"/create","/edit"}, params = "Action=go3",method = RequestMethod.GET)
public void download2(HttpServletResponse response,HttpServletRequest request) {

    try {
        java.io.File file=new java.io.File("C:\\RAHUL\\personal\\HDFC Bank Credit Card.pdf");
        InputStream in = new BufferedInputStream(new FileInputStream(file));

         response.setHeader("Content-Disposition", "attachment; filename=empfile.pdf");
        response.setContentType("application/pdf");

        request.setAttribute("override","Y");
         org.apache.commons.io.IOUtils.copy(in, response.getOutputStream());
        response.flushBuffer();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
blathur
  • 187
  • 1
  • 8
  • @Alexander,there is no download window or the output in jsp. Actually the frame work is expecting the view name as return from the function- Is it something related that or anything related to Jboss server or Filters? – blathur Mar 24 '15 at 09:18
  • What actually happens when you call the handler? – Alexander Mar 25 '15 at 13:16
  • @Alexander, control is reaching up to the method I created, and everything is fine inside method(response is set|) ,control returns from method-- after that control goes back to framework - then nothing.. – blathur Mar 25 '15 at 14:04