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