Java upload file and download file tool class

Source: Internet
Author: User
Tags save file

Upload the Download class below

The code is as follows Copy Code

public static void DownloadFile (String fileName, String downloadname, httpservletresponse response) {
try {
Response.setcontenttype ("Octets/stream");
Response.AddHeader ("Content-type", "text/html; Charset=utf-8 ");
String downloadname = new String (Downloadname.getbytes ("GBK"), "iso8859-1");
Response.AddHeader ("Content-disposition", "attachment;filename=" + downloadname);

FileInputStream FileInputStream = new FileInputStream (fileName);
OutputStream out = Response.getoutputstream ();
int i = 0;
while ((i = Fileinputstream.read ())!=-1) {
Out.write (i);
}
Fileinputstream.close ();
catch (Exception e) {
E.printstacktrace ();
}
}

/**
* Upload File
*
* @param file
* File
* @param radomfilename
* Save file name
* @param Materialfilepath
* Save Path
* @return
*/
public static Boolean FileUpload (Multipartfile file, String radomfilename,
String Materialfilepath) {
Boolean flag = true;
File Basefile = new file (Materialfilepath);
File Trainfile = new file (Basefile, radomfilename);
if (!basefile.exists ()) {
Basefile.mkdirs ();
}
try {
File.transferto (Trainfile);
catch (IllegalStateException e) {
Flag = false;
catch (IOException e) {
Flag = false;
}//Save File
return flag;
}

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.