File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in downloading file from server using Http Response

 
VijayRawat Rawat
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using this piece of code to download file from the server using Http response. Most of the time it works fine but sometime it gives error when it is downloaded and displayed in the browser(IE). Same thing works fine in another browser(IE of another machine)


public void downloadFile(String filePath, HttpServletResponse res) {

File fileToDownload = new File(filePath);
if (filePath.endsWith(".pdf")) {
res.setContentType("application/pdf");
}
else if(filePath.endsWith(".xls")) {
res.setContentType("application/vnd.ms-excel");
}
else if (filePath.endsWith(".doc")) {
res.setContentType("application/msword");
}
else {
res.setContentType("APPLICATION/OCTET-STREAM");
//res.setContentType("multipart/data-form");
System.out.println("Content type is multipart/data-form");
}
res.setHeader("Content-disposition",
"attachment; filename=" +
fileToDownload.getName());
res.setContentLength(fileToDownload.length());

OutputStream = null;
BufferedInputStream fileInputStream = null;
try {
fileInputStream = new BufferedInputStream(
FileInputStream(fileToDownload));
bytes[] b = new bytes[1024]
out = res.getOutputStream();
int i = 0;
while((i=fileInputStream.read(b))!=-1)
{
out.write(b);
}
}
catch(IOException ex) {
System.out.println("IOException :" +ex.getMessage());
}
finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
}
catch(IOException ex) {
System.out.println("Exception while closing FileInputStream :" +ex.getMessage());
}
}
if(out != null) {
out.close();
}
}
}
can any one know the cause of problem
Thanks in advance
vijay
 
Senthil B Kumar
Ranch Hand
Posts: 160
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What kind of error messages ?
 
Fisher Daniel
Ranch Hand
Posts: 582
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Could you please give us the errors you get?

I hope I can help you with this way...

thanks
daniel
 
Senthil B Kumar
Ranch Hand
Posts: 160
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using the following code, which is working perfectly for small size files. But when it comes to files of size 2 MB and more its giving Internal Server Error. The Server is a ORACLE 10g server.

The same code was tried in Weblogic 6.1, in which even 6 MB file is displayed easily.

IS THIS A NETWORK PROBLEM ? IS THERE ANY SOLN FOR THIS ?


String fileURL = "http://127.0.0.1:80/reports/test.pdf";
System.out.println("fileURL=="+fileURL);
URL url = new URL(fileURL);

BufferedInputStream bis = new BufferedInputStream(url.openStream());
ServletOutputStream out =response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(out);

byte[] buff = new byte[4096];
int bytesRead;

// Simple read/write loop.
while(-1 != (bytesRead = bis.read(buff, 0, buff.length)))
{

bos.write(buff, 0, bytesRead);

}
out.close();
bis.close();
bos.close();


04/12/08 11:40:28 Internal error in HttpServer

java.lang.InterruptedException
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at com.evermind.io.SingleReadBufferInputStream.readChunk(SingleReadBufferInputStream.java:116)
at com.evermind.io.SingleReadBufferInputStream.read(SingleReadBufferInputStream.java:35)
at com.evermind.server.http.AJPRequestHandler.readAJPPackets(AJPRequestHandler.java:462)
at com.evermind.server.http.AJPRequestHandler.initRequest(AJPRequestHandler.java:396)
at com.evermind.server.http.AJPRequestHandler.run(AJPRequestHandler.java:185)
at com.evermind.server.http.AJPRequestHandler.run(AJPRequestHandler.java:125)
at com.evermind.util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:192)
at java.lang.Thread.run(Thread.java:534)
04/12/08 11:59:45 9.0.4.0.0 Started
04/12/08 11:59:46 9.0.4.0.0 Started

What could be the reason ?
[ December 10, 2004: Message edited by: kolkata prince ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic