0

I'm trying to download a file using java, file is downloading but showing invalid characters in a file as shown in below.

Same thing with the images too, Showing invalid image.

Everything looks fine, Did i missed something? or Anybody having better solution than this?

index.jsp

<body>  
     Click on the link to download:
     <a href="download.jsp">Download Link</a>
</body>

download.jsp

<%
try {
    String filename = "C:/slcmimages/StudentDetails.xls"; // jpg, png, doc etc
    response.setContentType("APPLICATION/OCTET-STREAM");
    response.setHeader("Content-Disposition", "attachment; filename=" + filename);

    // transfer the file byte-by-byte to the response object
    File fileToDownload = new File(filename);
    response.setContentLength((int) fileToDownload.length());
    FileInputStream fileInputStream = new FileInputStream(fileToDownload);
    int i = 0;
    while ((i = fileInputStream.read()) != -1) {
        out.write(i);
    }
    fileInputStream.close();
} catch (Exception e) {
    e.printStackTrace();
}
%>

Actual File:

enter image description here

File after downloading:

enter image description here

Santosh DJ
  • 53
  • 2
  • 10

0 Answers0