wood burning stoves 2.0*
The moose likes Servlets and the fly likes Socket closed exception while downloading heavy files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Socket closed exception while downloading heavy files" Watch "Socket closed exception while downloading heavy files" New topic
Author

Socket closed exception while downloading heavy files

Ahmad Shajee
Greenhorn

Joined: Dec 22, 2008
Posts: 3
Hi All,

Objective: To create an excel file on runtime and make it available for download through a servlet
Problem : for files running into size (above 5 MB), i am getting the below exception

Exception in FileDownload
java.net.SocketException: Socket closed
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:104)
at java.net.SocketOutputStream.write(SocketOutputStream.java:141)
at weblogic.servlet.internal.ChunkUtils.writeChunkTransfer(ChunkUtils.java:234)
at weblogic.servlet.internal.ChunkUtils.writeChunks(ChunkUtils.java:207)
at weblogic.servlet.internal.ChunkOutput.flush(ChunkOutput.java:302)
at weblogic.servlet.internal.ChunkOutput.checkForFlush(ChunkOutput.java:377)
at weblogic.servlet.internal.ChunkOutput.write(ChunkOutput.java:247)
at weblogic.servlet.internal.ChunkOutputWrapper.write(ChunkOutputWrapper.java:125)
at weblogic.servlet.internal.ServletOutputStreamImpl.write(ServletOutputStreamImpl.java:184)

Code Details: I am using apache poi APIs for creating the excel sheet, writing it to a file in a temp location and then making it available for download through servlet.

This is happening randomly when file sizes increases approx 2 MB or 5MB.

Code (not able to attach file says .java files not allowed)
--------------------------------------------------

public class FileDownload {

public static void downloadExcel(HttpServletResponse response, String name
, HSSFWorkbook wb
) throws IOException
{
String fileName="";
String filepath = "C:/temp";
fileName = name + "_"+ String.valueOf(System.currentTimeMillis()) +".xls";

File fOut = new File(filepath+fileName);
FileOutputStream outFile = new FileOutputStream(fOut);
wb.write(outFile);
outFile.flush();
outFile.close();
long t1 = System.currentTimeMillis();
int sleepTime = 10000;
while(fOut.length() < wb.getBytes().length)
{
if((System.currentTimeMillis()-t1) > sleepTime)
{
System.out.println("File generation taking long(ms): " + (System.currentTimeMillis()-t1));
break;
}
}

System.out.println("Excel file generated ("+wb.getBytes().length+"b): " + fileName + ". Time taken(ms): " + (System.currentTimeMillis()-t1) + ". Size: " + fOut.length());

// put a sleep for above file to be completely flushed
// Sleep for 2000 ms
sleepTime = 2000;
if(fOut.length()/(1024*1024) > 1)
{
sleepTime = 5000;
}
t1 = System.currentTimeMillis();
while((System.currentTimeMillis()-t1) < sleepTime)
{
continue;
}
System.out.println("Flush Time: " + sleepTime + "ms");

//TODO the above file create should also be deleted after download complete
ServletOutputStream sos = null;
try
{
System.out.println("Start Download file: " + fileName);
downloadFile(fileName, name+".xls", filepath, response, sos);
System.out.println("File downloaded: " + name+".xls");
}
catch (IOException ex)
{
ex.printStackTrace();
}
finally
{
try
{
if(sos!=null)
{
sos.flush();
sos.close();
}
}
catch (IOException ex)
{
}
}

}

private static void downloadFile(String fileName, String downloadFilename, String filePath, HttpServletResponse response, ServletOutputStream sos) throws IOException
{

DataInputStream ins = null;
int leidos = 0;

File f = null;
try {
f = new File(filePath + fileName);
ins = new DataInputStream(new FileInputStream(f));

}
catch (Exception e) {
ins = null;
}
response.setContentType("application/octet-stream");
response.setContentType("Content-length:" + f.length());
response.setHeader("Content-Disposition",
"attachment; filename=" + downloadFilename);
response.setHeader ("Pragma", "public");

int bufSize = 1024;
long size = f.length()/(1024*1024);
if(size > 1 && size <= 5) // if file size > 1 MB
{
bufSize = bufSize*100;
System.out.println("File Size: " + f.length());
System.out.println("Increasing buffer size to: " + bufSize);
}
else if(size > 5) // if file size > 5 MB
{
bufSize = bufSize*500;
System.out.println("File Size: " + f.length());
System.out.println("Increasing buffer size to: " + bufSize);
}
byte buff[] = new byte[bufSize];

sos = response.getOutputStream();
while ( (leidos = ins.read(buff)) > 0) {
sos.write(buff,0,leidos);
}
ins.close();
}
}
--------------------------------------
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61657
    
  67

Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information.

Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ahmad Shajee
Greenhorn

Joined: Dec 22, 2008
Posts: 3
Hi All,

Objective: To create an excel file on runtime and make it available for download through a servlet
Problem : for files running into size (above 5 MB), i am getting the below exception

Exception in FileDownload
java.net.SocketException: Socket closed
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:104)
at java.net.SocketOutputStream.write(SocketOutputStream.java:141)
at weblogic.servlet.internal.ChunkUtils.writeChunkTransfer(ChunkUtils.java:234)
at weblogic.servlet.internal.ChunkUtils.writeChunks(ChunkUtils.java:207)
at weblogic.servlet.internal.ChunkOutput.flush(ChunkOutput.java:302)
at weblogic.servlet.internal.ChunkOutput.checkForFlush(ChunkOutput.java:377)
at weblogic.servlet.internal.ChunkOutput.write(ChunkOutput.java:247)
at weblogic.servlet.internal.ChunkOutputWrapper.write(ChunkOutputWrapper.java:125)
at weblogic.servlet.internal.ServletOutputStreamImpl.write(ServletOutputStreamImpl.java:184)

Code Details: I am using apache poi APIs for creating the excel sheet, writing it to a file in a temp location and then making it available for download through servlet.

This is happening randomly when file sizes increases approx 2 MB or 5MB.

Code (not able to attach file says .java files not allowed)
--------------------------------------------------

--------------------------------------
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18908
    
    8

You'll get that message when the client decides to stop receiving before you send all the data.

And you are going to great lengths to make sure that the download takes as much time as possible, as far as I can see. So it's quite likely that the person at the browser is going to say something like "This download must be broken, let's try again" and hit the refresh button.

You have far too much code. And it's doing far too much unnecessary processing, of which that loop where you spin until some time is elapsed is by far the worst culprit.

Don't write the data to a file and then read it back. Just write it directly to the servlet's output stream. That's all. About 70% of that code can be tossed out, or perhaps 80%.
Ahmad Shajee
Greenhorn

Joined: Dec 22, 2008
Posts: 3
Dear Paul,

Thanks for the reply.

Well the problem actually began when I was writing directly to servlet's output stream and the data size increases (> 5MB), then always i was getting this 'Socket closed' exception. that is why i chose to write to temp file before downloading.

the unneccsary code is there to allow the temp file to be completely written before streaming to servlet response.

but i assure you this is not the culprit.

if anybody has faced any similar problem then please let know.

Note: while on local i am not facing this problem but only on staging or integration environment.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61657
    
  67

Doctor: You have appendicitis. We must remove the appendix.

Patient: I'm sure that's not it. let me ask the other patients if they know anything.
 
Consider Paul's rocket mass heater.
 
subject: Socket closed exception while downloading heavy files