Changeset 3003


Ignore:
Timestamp:
Dec 6, 2006, 12:25:46 PM (16 years ago)
Author:
Nicklas Nordborg
Message:

References #454: File upload times out

Possible fix but have to be tested with slow connection.

Location:
branches/2.1.2
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/2.1.2/src/clients/web/net/sf/basedb/clients/web/fileupload/FileUpload.java

    r2691 r3003  
    3535import java.io.OutputStream;
    3636import java.io.IOException;
     37import java.net.SocketTimeoutException;
    3738
    3839import javax.servlet.ServletRequest;
     
    5253{
    5354
     55  /**
     56    Debug file upload.
     57  */
     58  private static final org.apache.log4j.Logger log =
     59    org.apache.log4j.LogManager.getLogger("net.sf.basedb.clients.web.fileupload.FileUpload");
     60 
     61  private boolean debugEnabled = log.isDebugEnabled();
     62
     63  /**
     64    The default timeout to use when waiting for the next data packet to
     65    arrive.
     66  */
     67  public static final long DEFAULT_TIMEOUT = 20000;
     68 
    5469  private static final String EMPTY_STRING = "";
    5570 
     
    91106  private int transferRate = 0;
    92107
     108  /**
     109    The timeout in milliseconds that we wait for more data to arrive.
     110    Default value is 20000 = 20 seconds.
     111  */
     112  private long timeout = DEFAULT_TIMEOUT;
     113 
    93114  /**
    94115    Create a new <code>FileUpload</code> object which will read the posted
     
    103124    throws IOException, UploadAbortedException
    104125  {
    105     this(request, 0);
     126    this(request, 0, DEFAULT_TIMEOUT);
    106127  }
    107128
     
    113134    @param request The <code>ServletRequest</code> object containg information
    114135      about the request (and file upload)
    115     @param transferRate The maximum transfer rate in bytes per second
     136    @param transferRate The maximum transfer rate in bytes per second, or 0
     137      to transfer as fast as possible
     138    @param timeout The timeout in milliseconds to wait for the next packet
     139      of data to arrive
    116140    @throws IOException If there is an error reading from the request stream
    117141    @throws UploadAbortedException If the upload was aborted
    118142  */
    119   public FileUpload(ServletRequest request, int transferRate)
     143  public FileUpload(ServletRequest request, int transferRate, long timeout)
    120144    throws IOException, UploadAbortedException
    121145  {
     
    124148    this.status = new UploadStatus();
    125149    this.transferRate = transferRate;
     150    this.timeout = timeout;
    126151    String contentType = request.getContentType();
    127152    String sectionBoundary = "--"+contentType.substring(9+contentType.indexOf("boundary="));
     
    452477  {
    453478    byte[] line = new byte[1000];
    454     int bytes = in.readLine(line, 0, line.length);
     479    int bytes = readLine(line, 0, line.length);
    455480    if (bytes != -1)
    456481    {
     
    467492    else
    468493    {
    469       status.eof = true;
    470       in.close();
    471494      return EMPTY_STRING;
    472495    }
     
    491514    {
    492515      if (progress.hasAborted()) throw new UploadAbortedException();
    493       bytes = in.readLine(line, 0, line.length);
    494       if (bytes == -1)
    495       {
    496         status.eof = true;
    497       }
    498       else
     516      bytes = readLine(line, 0, line.length);
     517      if (bytes != -1)
    499518      {
    500519        progress.addTransferredBytes(bytes);
     
    532551    {
    533552      if (progress.hasAborted()) throw new UploadAbortedException();
    534       bytes = in.readLine(line, 0, line.length);
    535       if (bytes == -1)
    536       {
    537         status.eof = true;
    538         in.close();
    539       }
    540       else
     553      bytes = readLine(line, 0, line.length);
     554      if (bytes != -1)
    541555      {
    542556        progress.addTransferredBytes(bytes);
     
    606620    }
    607621  }
     622
     623  /**
     624    Read next line of data into a buffer.
     625    @return The number of bytes read or -1 if the end is reached
     626    @since 2.1.2
     627  */
     628  private int readLine(byte[] b, int off, int len)
     629    throws IOException
     630  {
     631    int bytes = 0;
     632    boolean gotData = false;
     633    long startTime = System.currentTimeMillis();
     634    while (!gotData)
     635    {
     636      try
     637      {
     638        if (debugEnabled) log.debug("Calling in.readLine() for more data");
     639        bytes = in.readLine(b, off, len);
     640        if (debugEnabled) log.debug("Got " + bytes + " bytes");
     641        gotData = true;
     642      }
     643      catch (SocketTimeoutException ex)
     644      {
     645        long waitTime = System.currentTimeMillis() - startTime;
     646        log.debug("SocketTimeoutException after " + waitTime + "ms", ex);
     647        if (waitTime > timeout)
     648        {
     649          SocketTimeoutException ex2 =
     650            new SocketTimeoutException("No data for " + waitTime + " milliseconds");
     651          ex2.initCause(ex);
     652          log.debug("Total timeout exceeded after" + waitTime + "ms", ex2);
     653          throw ex2;
     654        }
     655        else
     656        {
     657          log.debug("Sleeping 1000ms");
     658          try
     659          {
     660            Thread.sleep(1000);
     661          }
     662          catch (Throwable t)
     663          {}
     664        }
     665      }
     666    }
     667    if (bytes == -1)
     668    {
     669      status.eof = true;
     670      in.close();
     671    }
     672    return bytes;
     673  }
     674 
    608675 
    609676  /**
     
    793860     
    794861      int boundaryLength = boundary.length;
    795       int bytes = 0;
    796       bytes = in.readLine(nextBuffer, 0, nextBuffer.length);
    797 
    798       if (bytes == -1)
    799       {
    800         status.eof = true;
    801         in.close();
    802       }
    803       else
     862      int bytes = readLine(nextBuffer, 0, nextBuffer.length);
     863
     864      if (bytes != -1)
    804865      {
    805866        progress.addTransferredBytes(bytes);
  • branches/2.1.2/www/filemanager/upload/upload.jsp

    r2753 r3003  
    7474try
    7575{
    76   upload = new FileUpload(request, maxTransferRate);
     76  upload = new FileUpload(request, maxTransferRate, FileUpload.DEFAULT_TIMEOUT);
    7777  final FileUploadProgress progress = upload.getProgress();
    7878  sc.setSessionSetting("FileUploadProgress", progress);
Note: See TracChangeset for help on using the changeset viewer.