Java detects the upload progress of FTP files using the Comms-net jar package

Source: Internet
Author: User
Tags ftp file

This article only describes the general idea and the function corresponding to some development environment, the specific implementation please combine their own development situation, for reference only, if there is wrong place, welcome everyone point out!

Prepare the environment: JDK1.7 or 1.8, Eclipse, FTP server (self-erecting), Comms-net jar Package 3.3 version. The rest is not listed in detail.

1. In the real development of the FTP server and the deployment of the Project Server is basically not the same, so the basic Springmvc file upload progress gets, can only get to the file of the binary stream to reach the project background progress. For real FTP file upload progress, you need to use the comms-net provided by the listener to implement.

2. Set up the listener before uploading the FTP code. To complete the file upload progress monitoring, my side is to keep progress in the session, the front-end timing acquisition. When the upload is complete, clear the corresponding values in the session. Attach the core code:

/*** Get File Upload progress *@paramtotalbytes Total File Size*/     Public voidUploadprogress (Longtotalbytes) {Copystreamadapter Streamlistener=NewCopystreamadapter () {/**             * @paramtotalbytestransferred number of bytes already uploaded*/@Override Public voidBytestransferred (LongTotalbytestransferred,intBytestransferred,Longstreamsize) {                /**get the upload progress by calculating the percentage of uploaded size and total size. */String percent= Calculationutil.percentcal ((int) Totalbytestransferred, (int) TotalBytes, 3); if(Percent.contains (".") ) {percent= percent.substring (0, Percent.lastindexof ("."))); } HttpSession Session=request.getsession ();            Session.setattribute (Session.getid (), percent);         }         };  This. Client.setcopystreamlistener (Streamlistener); }

Java detects the upload progress of FTP files using the Comms-net jar package

Related Article

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.