Changeset 3003
- Timestamp:
- Dec 6, 2006, 12:25:46 PM (16 years ago)
- 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 35 35 import java.io.OutputStream; 36 36 import java.io.IOException; 37 import java.net.SocketTimeoutException; 37 38 38 39 import javax.servlet.ServletRequest; … … 52 53 { 53 54 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 54 69 private static final String EMPTY_STRING = ""; 55 70 … … 91 106 private int transferRate = 0; 92 107 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 93 114 /** 94 115 Create a new <code>FileUpload</code> object which will read the posted … … 103 124 throws IOException, UploadAbortedException 104 125 { 105 this(request, 0 );126 this(request, 0, DEFAULT_TIMEOUT); 106 127 } 107 128 … … 113 134 @param request The <code>ServletRequest</code> object containg information 114 135 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 116 140 @throws IOException If there is an error reading from the request stream 117 141 @throws UploadAbortedException If the upload was aborted 118 142 */ 119 public FileUpload(ServletRequest request, int transferRate )143 public FileUpload(ServletRequest request, int transferRate, long timeout) 120 144 throws IOException, UploadAbortedException 121 145 { … … 124 148 this.status = new UploadStatus(); 125 149 this.transferRate = transferRate; 150 this.timeout = timeout; 126 151 String contentType = request.getContentType(); 127 152 String sectionBoundary = "--"+contentType.substring(9+contentType.indexOf("boundary=")); … … 452 477 { 453 478 byte[] line = new byte[1000]; 454 int bytes = in.readLine(line, 0, line.length);479 int bytes = readLine(line, 0, line.length); 455 480 if (bytes != -1) 456 481 { … … 467 492 else 468 493 { 469 status.eof = true;470 in.close();471 494 return EMPTY_STRING; 472 495 } … … 491 514 { 492 515 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) 499 518 { 500 519 progress.addTransferredBytes(bytes); … … 532 551 { 533 552 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) 541 555 { 542 556 progress.addTransferredBytes(bytes); … … 606 620 } 607 621 } 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 608 675 609 676 /** … … 793 860 794 861 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) 804 865 { 805 866 progress.addTransferredBytes(bytes); -
branches/2.1.2/www/filemanager/upload/upload.jsp
r2753 r3003 74 74 try 75 75 { 76 upload = new FileUpload(request, maxTransferRate );76 upload = new FileUpload(request, maxTransferRate, FileUpload.DEFAULT_TIMEOUT); 77 77 final FileUploadProgress progress = upload.getProgress(); 78 78 sc.setSessionSetting("FileUploadProgress", progress);
Note: See TracChangeset
for help on using the changeset viewer.