Upload a file with a progress bar (Java + Ajax, with source code) [original]

Source: Internet
Author: User
I read ahxu's article about the progress of uploading files and understood the principle. Because ahxu is not open-source, I had to write it myself for future extension, otherwise, I will not re-invent the wheel.
First of all, go to the Apache official website under the latest commons-fileupload-1.1.1.jar and its source code, looked at it, found some discrepancies with the original version, carefully looked at the source code of the commons-fileupload-1.1.1, understand its entire process, in inputstream, you can get the progress. Here, I want to thank ahxu for his contribution.
OK, start to the topic. In the new version, we use servletfileupload, read the source code, write a class similar to it, and just overwrite the parserequest method, and attach some methods from ahxu's brother.
When writing code, you also need to use the servlet package servlet-api.jar.
When debugging is completed, the following error occurs: Java. lang. noclassdeffounderror: ORG/Apache/commons/IO/output/deferredfileoutputstream, then a query to know the less commons-io-1.2.jar, plus, it is OK.
On the acquisition and display progress, I used a self-written Ajax application architecture, extracted part of the code from it, note that JS files only apply to the UTF-8 coding page, because now I only write UTF-8 coding page, International convenience.
The following is an example.
  
The source code is provided below:
Process uploaded files
Example
Progressupload. jar contains the commons-fileupload-1.1.1.jar, servlet-api.jar, commons-io-1.2.jar, in progressupload. jar, it also contains an attribute file that contains some default configurations that can be modified as needed.
Because of the source code, you can view the source code on your own. In addition, I wrote it in that example. I will not go into details here. In this example, only fileitem is obtained, and then the item is saved. The sample code is as follows:

File upfile = new file (remotefilepath );
Try
{
Item. Write (upfile );
Fileinfo. Put ("infotip", "uploaded successfully .");
}
Catch (Java. Lang. Exception E)
{
Fileinfo. Put ("infotip", "Upload Failed .");
}

If you have any new ideas, please leave a message to me. It is the best principle to make this thing a good job and develop it together.

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.