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:
File after downloading: