A simple java-based file upload and download function

Source: Internet
Author: User

recently in the company to the client interface, there is a picture upload and file download function, originally wanted to use struts to do file upload download, but looked at the next company does not seem to have this configuration, and then looked at the next Colleague's code, only to find that the original Apache can also be a simple implementation of file upload Download.
First introduce Commons-io-2.2.jar
Fileutils gives us a lot of ways to manipulate files, such as uploading files across folders, uploading individual files, etc.
then, Look at the code:
public static void UploadFile (String targetdirectory,string targetfilename,file File) {
try {
File target = new file (targetdirectory, targetfilename);
/**
* Upload entire folder file
* */
Fileutils.copydirectory (file,target);
/**
* Upload a single file
* */
Fileutils.copyfile (file, target);
} catch (ioexception E) {
TODO auto-generated Catch block
E.printstacktrace ();
}


so far a simple file upload is Complete. Next look at the download file:
public static void DownloadURL (String httpurl,string Targetdirectory) {
try {
Fileutils.copyurltofile (new URL (httpurl),
New File (targetdirectory));
} catch (malformedurlexception E) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (ioexception E) {
TODO auto-generated Catch block
E.printstacktrace ();
}


and then a man method is tested Correctly:

public static void main (string[] Args) {
Upload
File File = new file ("d:/tp/xiazai.png");
Fileutil.uploadfile ("d:/test/", "uplod2016.png", file);

Download
Fileutil.downloadurl ("http://img5.pcpop.com/ArticleImages/500x375/1/1613/001613973.jpg", "d:/test/jams.png");

Upload success:
650) this.width=650; "src=" http://dl2.iteye.com/upload/attachment/0121/4424/ F186f57c-7eb5-393c-96be-1ce3ef609381.png "style=" Border:0px;color:rgb (85,85,85); white-space:normal; Background-color:rgb (255,255,255); font-family:helvetica, Tahoma, Arial, sans-serif;font-size:14px; "/> 
Download Successful:

650) this.width=650; "src=" http://dl2.iteye.com/upload/attachment/0121/4427/ 226371a7-fcb7-3a42-be5c-a75a13d6f0f3.png "style=" Border:0px;color:rgb (85,85,85); white-space:normal; Background-color:rgb (255,255,255); font-family:helvetica, Tahoma, Arial, sans-serif;font-size:14px; "/> 
Perfect.

give the source code, hope to work with your brothers to Progress.

See annex


Originally written in iteye, found to be csdn a friend reproduced, also forget, incredibly also implanted ads, now offer original link and source a copy, hope a total progress.



This article from the "12174301" blog, reproduced please contact the author!

A simple java-based file upload and download function

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.