0

I am trying to download a file in browser using java. The problem is i am not getting any error in code . but the file is not getting download in the browser. I have reffrenced this site : http://www.codejava.net/frameworks/spring/spring-mvc-sample-application-for-downloading-file.

        ServletContext context = request.getServletContext();
        String appPath = context.getRealPath("");
        String filePat="pat of the file";
        File downloadFile = new File(filePath);
        System.out.println("downloadFile path: "+ filePath);
        FileInputStream inputStream = new FileInputStream(downloadFile);

     // get MIME type of the file
        String mimeType = context.getMimeType(fullPath);
        if (mimeType == null) {
            // set to binary type if MIME mapping not found
            mimeType = "application/octet-stream";
        }
        System.out.println("MIME type: " + mimeType);

        response.setContentLength((int) downloadFile.length());

        // set headers for the response
        String headerKey = "Content-Disposition";
        String headerValue = String.format("attachment; filename=\"%s\"",downloadFile.getName());
        response.setHeader(headerKey, headerValue);

        OutputStream outStream = response.getOutputStream();

        byte[] buffer = new byte[BUFFER_SIZE];
        System.out.println("buffer: "+ buffer.length);
        int bytesRead = -1;

        // write bytes read from the input stream into the output stream
        int counter=0;
        while ((bytesRead = inputStream.read(buffer))!=-1  ) {
            counter++;
            System.out.println("counter: "+ counter+ "bytesRead:"+bytesRead);
            outStream.write(buffer, 0, bytesRead);
        }

        inputStream.close();
        outStream.close();
Eagle
  • 217
  • 1
  • 2
  • 15
  • The code has two magic numbers and the writing code is broken. I'd copy/paste code from another site if I were you. – Gimby Oct 27 '15 at 18:47
  • I have added 1895 just to check. As counter value > 1896 was giving writebeyondcontent length error. – Eagle Oct 27 '15 at 18:51

1 Answers1

0

It may be because you are adding 1000 more byes to the content length. So, the browser is actually waiting to get all the bytes as mentioned in the Content-Length header.

Ramesh PVK
  • 15,200
  • 2
  • 46
  • 50
  • There is also a bug in the writing code, it writes buffer.length bytes instead of bytesRead bytes. – Gimby Oct 27 '15 at 19:05
  • I have removed that 1000 extra bytes now. but no luck. i have updated the code snippet too. – Eagle Oct 27 '15 at 19:16
  • You should try using an existing library. http://stackoverflow.com/questions/1442893/implementing-a-simple-file-download-servlet – Ramesh PVK Oct 27 '15 at 19:34
  • Hey, I found that , The data in coming to the browser but as a stream . Means I can see the file data in the success block of the ajax call. but what the requirment is to download the file in the browser... – Eagle Oct 29 '15 at 06:25
  • Thanks Ramesh,Gimby for clarifying the java code. I was trying to do the download from ajax call in javascript but I came to kow its not poassible . So used window.location.href and my code is woking perfectly. I have shared my code in this link "http://stackoverflow.com/questions/23775503/how-to-download-file-in-browser-using-spring-mvc/33429560#33429560".... – Eagle Oct 30 '15 at 06:12