Ahmad Shajee

Greenhorn
+ Follow
since Dec 22, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ahmad Shajee

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.
13 years ago
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)
--------------------------------------------------

--------------------------------------
13 years ago
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();
}
}
--------------------------------------
13 years ago