Reputation: 17
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("Hi inside download file :");
response.setContentType("application/vnd.ms-excel");
PrintWriter out = response.getWriter();
String filename = "ResetPassword.xlsx";
InputStream is = null;
//is = this.getClass().getClassLoader().getResourceAsStream("LoginConfigurations.xlsx");
//System.out.println("InputStream :"+is);
String filepath = "E:\\SPACE_OM_01.02\\SPACE-OM_1.0-V01.02\\WebContent\\Data\\";
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
// use inline if you want to view the content in browser, helpful for
// pdf file
response.setHeader("Content-Disposition","inline; filename=\"" +filename + "\"");
FileInputStream fileInputStream = new FileInputStream(filepath+ filename);
/*BufferedReader br = new BufferedReader(new InputStreamReader(is));
int line;
System.out.println("Buffer Reader length :"+br.read());
StringBuilder sb = new StringBuilder();
while ((line = br.read()) != -1) {
//sb.append(line);
out.write(line);
}
br.close();*/
int line;
System.out.println("Buffer Reader length :"+fileInputStream.read());
//StringBuilder sb = new StringBuilder();
while ((line = fileInputStream.read()) != -1) {
//sb.append(line);
out.write(line);
}
fileInputStream.close();
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
I am trying to download the excel file with extension .xlsx using button click after clicking download button file is downloading when i am trying to open that download file its giving error like Excel file cannot open because the file format or file extensions is not valid. Please help me in solving this problem. Thanks in advance.
Upvotes: -1
Views: 1827
Reputation: 21
I faced same issue few days back, and it got resolved after adding <nonFilteredFileExtensions>
in maven dependency as below:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<encoding>UTF-8</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>xls</nonFilteredFileExtension>
<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
This will prevent binary files filtering.
Upvotes: 0
Reputation: 31269
Replace this line:
PrintWriter out = response.getWriter();
with this line:
OutputStream out = response.getOutputStream();
You're reading a byte from an InputStream and you should be writing a byte to an OutputStream again to copy it. Right now your byte is sent as a character to a PrintWriter which is wrong.
And remove this line:
System.out.println("Buffer Reader length :"+fileInputStream.read());
This line is reading the first byte from the file and throwing it away, and as a result the downloaded file is missing its first byte.
Upvotes: 1