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 see the code download :
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;font-family:helvetica, Tahoma, Arial, Sans-serif; Font-size:14px;line-height:25.2px;white-space:normal;background-color:rgb (255,255,255); "Alt=" F186f57c-7eb5-393c-96be-1ce3ef609381.png "/>
Download Successful:
650) this.width=650; "Src=" http://dl2.iteye.com/upload/attachment/0121/4427/ 226371a7-fcb7-3a42-be5c-a75a13d6f0f3.png "style=" Border:0px;font-family:helvetica, Tahoma, Arial, Sans-serif; Font-size:14px;line-height:25.2px;white-space:normal;background-color:rgb (255,255,255); "Alt=" 226371a7-fcb7-3a42-be5c-a75a13d6f0f3.png "/>
Perfect.
A simple Java-based file upload and download function