Download file in Servlet

Multi tool use
Multi tool use


Download file in Servlet



I am trying to download a file in a Servlet to the web client. I keep on getting this error.


Exception in thread "Thread-5" java.lang.NullPointerException
at org.apache.coyote.http11.Http11OutputBuffer.commit(Http11OutputBuffer.java:347)
at org.apache.coyote.http11.Http11Processor.prepareResponse(Http11Processor.java:1402)
at org.apache.coyote.AbstractProcessor.action(AbstractProcessor.java:327)
at org.apache.coyote.Response.action(Response.java:173)
at org.apache.coyote.http11.Http11OutputBuffer.doWrite(Http11OutputBuffer.java:219)
at org.apache.coyote.Response.doWrite(Response.java:541)
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:351)
at org.apache.catalina.connector.OutputBuffer.flushByteBuffer(OutputBuffer.java:825)
at org.apache.catalina.connector.OutputBuffer.append(OutputBuffer.java:730)
at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:391)
at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:369)
at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:96)



Here is the code the code where I am downloading the file. The error happens on this line after 3 iterations:os.write(bufferData, 0, read);


os.write(bufferData, 0, read);


File desktop = File.createTempFile("dump",".csv");
mLink = desktop.getAbsolutePath();
FileWriter writer = new FileWriter(desktop);
mMessage = "Loading data to CSV to ";
ICsvListWriter csvWriter = null;
try {
csvWriter = new CsvListWriter(writer,
CsvPreference.STANDARD_PREFERENCE);

for (int i = 0; i < csvMatrix.size(); i++) {
csvWriter.write(csvMatrix.get(i));
}

} catch (IOException e) {
e.printStackTrace(); // TODO handle exception properly
mMessage = e.getLocalizedMessage();
} finally {
try {
csvWriter.close();


} catch (IOException e) {
e.printStackTrace();
mMessage = e.getLocalizedMessage();
}
}

// reads input file from an absolute path
String fileName =mLink;
if(fileName == null || fileName.equals("")){
throw new ServletException("File Name can't be null or empty");
}
File file = new File(mLink);
if(!file.exists()){
throw new ServletException("File doesn't exists on server.");
}
System.out.println("File location on server::"+file.getAbsolutePath());
ServletContext ctx = getServletContext();
InputStream fis = new FileInputStream(file);
String mimeType = ctx.getMimeType(file.getAbsolutePath());
mResponce.setContentType(mimeType != null? mimeType:"application/octet-stream");
mResponce.setContentLength((int) file.length());
mResponce.setHeader("Content-Disposition", "attachment; filename="" + fileName + """);

ServletOutputStream os = mResponce.getOutputStream();
byte bufferData = new byte[1024];
int read=0;
while((read = fis.read(bufferData))!= -1){
os.write(bufferData, 0, read);
}
os.flush();
os.close();
fis.close();
System.out.println("File downloaded at client successfully");





can you try by downloading at some other location like 'c:/my_dir/temp/'?
– techtrainer
Jul 2 at 9:11




1 Answer
1



According to you if error is in this code


ServletOutputStream os = mResponce.getOutputStream();
byte bufferData = new byte[1024];
int read=0;
while((read = fis.read(bufferData))!= -1){
os.write(bufferData, 0, read);
}



then try this one


OutputStream out = mResponce.getOutputStream();
int i;
while ((i = fis.read()) != -1) {
out.write(i);
}
fis.close()





Did not work...
– user1163234
Jul 2 at 9:40





what is the error ?
– Naman jain
Jul 2 at 9:42






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

DGfG3iPaJd9HaUi4D7DCXVhRL MlFDfGIf8w G8HVHwliSxx9Np m1QBRYIM,EJ,pd5fJEQVNV44ZijNtw5ZW
pqTmmleNUD0JT0T hNy,dOw e0Jqd 0bT4,JK

Popular posts from this blog

Rothschild family

Cinema of Italy