A bug in a small program with real-time feedback on upload speed

Source: Internet
Author: User

A bug in a small program with real-time feedback on upload speed

 
 
  1. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  2. // TODO Auto-generated method stub
  3. int LENGTH=10240;
  4. InputStream is=request.getInputStream();
  5. String fn=UUID.randomUUID().toString();
  6. File a=new File(HlsConfigure.getPath()+fn);
  7. byte []content=new byte[LENGTH];
  8. int length;
  9. OutputStream os =new FileOutputStream(a);
  10. PrintWriter out = response.getWriter();
  11. out.write("
  12. long total=0;
  13. long vb=System.currentTimeMillis();
  14. again:
  15. while(true){
  16. int total_length=0;
  17. long begin=System.currentTimeMillis();
  18. while((length=is.read(content))!=-1){
  19. os.write(content, 0, length);
  20. System.out.println("get "+length);
  21. total_length+=length;
  22. if(total_length>=200*1024){
  23. break;
  24. }
  25. }
  26. System.out.println("will write speed");
  27. long time=System.currentTimeMillis()-begin;
  28. out.write("<tr><td>");
  29. out.write(Integer.toString(total_length));
  30. out.write("</td><td>");
  31. out.write(Double.toString(total_length/(1.0240)/(time)));
  32. out.write("</td></tr>");
  33. total+=total_length;
  34. out.flush();
  35. if(length==-1)
  36. break;
  37. }
  38. os.close();
  39. long time=System.currentTimeMillis()-vb;
  40. out.write("<tr><td>");
  41. out.write(Long.toString(total));
  42. out.write("</td><td>");
  43. String speed=Double.toString(total/(1.0240)/(time));
  44. out.write(speed);
  45. out.write("</td></tr>");
  46. out.flush();
  47. out.write("</table></body>
  48. System.out.println("from "+request.getRemoteAddr()+ " speed is "+speed);
  49. // a.delete();
  50. }

The code is simple:
1. Open inputstream, read the stream, and write it to the file;
2. Feedback on the upload speed per K;
3. The client segment uses the simplest input type = 'file' to upload files;

However, when the customer uploads a large file, the hang will not be completed.
The reason is:
1. When the Browser fails to send the request, it will not read the server output. The idea of displaying the upload speed in real time is incorrect;
2. From the code execution logic, the client segment uploads and the service segment receives data, which does not cause the server buffer to be full;
3. However, the data returned by the server to the customer segment is not read from the buffer zone due to the reason of 1. Therefore, the buffer of the customer segment will be full;
4. As a result, 28 rows of write cannot be written;
5. Because the server is blocked in the write field, the server will not read any more data.
6. The server's buffer zone is full, so the client's upload will be hung.

Note:
ACK packets are implemented by the tcp protocol stack. Therefore, ACK is not affected regardless of whether the program reads the data in the buffer zone. If not all data is written, the window value will decrease. This flag can be used to indicate whether the program has not read data;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.