Recently in the project involved in the file upload, download, before learning IO did not get much clearer, on the Internet to find some examples of uploading and downloading, and then modified the section. After testing, upload download files can be used temporarily, the following is the method of uploading and downloading:
1. Upload files locally to the server
HTML code:
<formID= "Uploaddatuminfo"name= "Uploaddatuminfo"Method= "POST"enctype= "Multipart/form-data"Target= "Target_upload"><!--Note To add: enctype= "Multipart/form-data" - <inputtype= "File"ID= "Datum_address"name= "Datum_address"class="" /> </form>
JS Code:
function Uploaddatum () {
<!--Verify that the uploaded file is empty-to-Var datum_address=$ ("#datum_address"). Val (); if (datum_address== ') {alert ("Upload file cannot be empty! "); Return } <!--Submit form Data -document.uploaddatuminfo.action= "<%=Webconstants.web_root%>/saveuploadmydatum.do?&<%=mctsutils.geturlrandom ()%>"; Document.uploadDatumInfo.submit ();}
Backstage code:@Controller
@Controller
Public classMycontroller {@Resource (name= "MyService")PrivateMyService MyService;PrivateResourceManager RM =resourcemanager.getinstance ();//Save upload data information@RequestMapping (value = "/saveuploadmydatum.do") Public voidSaveuploadmydatum (httpservletrequest request, httpservletresponse response,FinalModelmap context, Vcdatuminfo vcdatuminfo)throwsException {Try{String MyDir= Rm.getvalue ("My_file_path"); String Filedir= Rm.getvalue ("My_space_path"); File Sourcefolder=NewFile (MyDir + "/" +filedir); Create a storage folderif(!sourcefolder.exists ())
{Sourcefolder.mkdirs (); } String datum_address= ""; Diskfileitemfactory Factory=Newdiskfileitemfactory (); Factory.setsizethreshold (2048 * 1024); Uploadprogresslistener Getbarlistener=NewUploadprogresslistener (Request); Servletfileupload Upload=Newservletfileupload (Factory); Upload.setprogresslistener (Getbarlistener); List<Object> formlist =upload.parserequest (Request); Iterator<Object> FormItem =Formlist.iterator (); while(Formitem.hasnext ())
{Fileitem Item=(Fileitem) formitem.next (); if(!Item.isformfield ())
{ if(Item! =NULL&& item.getsize () > 0)
{String FieldName=Item.getfieldname (); String timetemp= String.valueof (NewDate (). GetTime ()); String SubName=item.getname (). substring (Item.getname (). LastIndexOf (".", Item.getname (). Length ()); /Gets the suffix name of the uploaded file String realname= Item.getname (). substring (0, Item.getname (). LastIndexOf (".") ;//Get the file name of the uploaded files//System.out.println (realname); if(Fieldname.equals ("Datum_address"))
{datum_address= Filedir + "/" + "datum_" + timetemp + "_" + Realname +subname;//The address of the file to be saved to the database
File File = new file (MyDir + "/" + datum_address);//Create Save path
Item.write (file);//write Files data
} }
}
}
}
2. Downloading files from the server
Background code:
String MyDir = Rm.getvalue ("My_file_path"); OutputStream toclient=NULL; String Path= ""; Path= MyDir + "/" +vcdatuminfo.getdatum_address (); Try { //Path refers to the paths of the files you want to download. File File =NewFile (path); //Get file nameString filename =File.getname (); //gets the suffix name of the file. String ext = filename.substring (Filename.lastindexof (".") + 1); String Realname= File.getname (). substring (File.getname (). LastIndexOf ("_") + 1, File.getname (). LastIndexOf (".") ;//Get the filename, this step gets the file name is probably not the original file name when uploading String realfilename= Realname + "." +ext; //download the file as a streamInputStream FIS =NewBufferedinputstream (NewFileInputStream (path)); byte[] buffer =New byte[Fis.available ()]; Fis.read (buffer); Fis.close (); //Empty ResponseResponse.reset (); //set the header of the responseResponse.AddHeader ("Content-disposition", "attachment;filename=" +Java.net.URLEncoder.encode (Realfilename, "UTF-8") ///Note This step to set the file name, Chinese is prone to garbled response.addheader ("Content-length", "" "+file.length ()); Toclient=NewBufferedoutputstream (Response.getoutputstream ()); Response.setcontenttype ("Application/octet-stream"); Toclient.write (buffer); Vcdatuminfo.setdownload_number (Vcdatuminfo.getdownload_number ()+ 1); Vcspaceservice.updatedatuminfo (Vcdatuminfo); } Catch(IOException ex) {ex.printstacktrace (); }finally { if(Toclient! =NULL) {Toclient.flush (); Toclient.close (); } }
Finally, thank you for sharing examples of the author!
Upload files locally to the server, download files from the server to local