wood burning stoves*
The moose likes Servlets and the fly likes Problem in downloading file from server using Http Response Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem in downloading file from server using Http Response" Watch "Problem in downloading file from server using Http Response" New topic
Author

Problem in downloading file from server using Http Response

VijayRawat Rawat
Greenhorn

Joined: Aug 19, 2004
Posts: 9
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

Joined: Feb 09, 2004
Posts: 140
What kind of error messages ?


Work like you don't need the money. Love like you've never been hated. Dance like nobody's watching. Sing like nobody's listening. Live like it's Heaven on Earth.
Currently I Reside Here WEBlog
Fisher Daniel
Ranch Hand

Joined: Sep 14, 2001
Posts: 582
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

Joined: Feb 09, 2004
Posts: 140
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Problem in downloading file from server using Http Response
 
Similar Threads
error in dwonloading a pdf file
how to download file from server through servlet
Display of PDF file through a servlet?
showing pdf in Fire fox giving problem
fileREader and FileWriter