General J2EE: Urgent : java.net.SocketException in Download Servlet

  1. This is my code. while downloading a file, sometimes i am getting exception :"java.net.SocketException: Connection reset by peer: socket write error"

    do i need to include flush() method?
    is there any problem setting buffersize 10248?

    if problem is because of this. then can u pls explain the reason also?

    Any ideas/code that accomplishes this issue will be highly appreciated :)

    import java.net.*;
    import java.io.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.sql.*;

    public class Download extends HttpServlet {
          HttpSession session =null;
          File F;
          BufferedInputStream fif;
          ServletOutputStream stream;
         public void init(){
           stream =null;
        public void doGet(HttpServletRequest request ,HttpServletResponse response) {
               // Initializing objects
                // Setting Buffer size
                int buffersize;
                String cfile="";
                boolean flag=false;
                session = request.getSession(true);
                //Receiving variables
                String fileName= request.getParameter("hid_objName");
                String filePath = request.getParameter("filepath");
                String downloadFile = filePath + fileName;
                F=new File(downloadFile);
                String file=F.getName();
                buffersize= 10248; // (int)F.length();
                byte b[]=new byte[buffersize];
                // Setting Content Type
                //Object for DML Bean
                DMLBean objDML = new DMLBean();
                //Servet output stream to download file
                stream = response.getOutputStream();

                //int data=1;
                fif = new BufferedInputStream(new FileInputStream(F));
                //writing data to output stream
                int count=fif.read(b,0,buffersize);

                // Update Database
                //Code to update the database

                //closing objects
                  if(fif!=null) fif.close();
                  if(stream!=null) {
                  if(F.exists()) F.delete();

                }catch(SocketException se){
                   if(fif!=null) fif.close();
                   if(stream!=null) {
                   if(F.exists()) F.delete();
                   System.out.println("SocketException " +se);
                catch(IOException io){
                   if(fif!=null) fif.close();
                   if(stream!=null) {
                   if(F.exists()) F.delete();
                   System.out.println("IOException " + io);
                catch(Exception e){
                   if(fif!=null) fif.close();
                   if(stream!=null) {
                   if(F.exists()) F.delete();
                   System.out.println("Exception " +e);
           }catch(Exception oe){}
  2. Hi Buddy,

        I am doing a similiar kind of thing and I am getting the same SocketException..

         How did u handle this..? I think, it would be helpfull to me..

    Thanks in Advance..