Recently in a project to do a SSM, encountered the addition of attachments and download features, in the online search a lot of information, found that many are not good use, after groping, found a set of simple methods, and share with you.
1. File Upload
The front page uses Easyui, the code is as follows:
<table><tr><td> accessories </td><td><input class= "Easyui-filebox" type= "file" Name= "File1" Id= "File1" ><a href= "Javascript:imageupload ()" class= "Easyui-linkbutton" > Upload </a></td><td ><input type= "hidden" id= "Ssfile" name= "Ssfile" > <!--for file name Echo--></td></tr><table
JS method call Backstage:
function Imageupload () {var file1 = document.getElementById ("File1"); var ssfile = document.getElementById ("Ssfile"); Ssfile.value = file1.value.substring (n); Remove the file name and assign a value echo to the text box, which is used to pass the file name $.ajaxfileupload to the background ({ URL: ' ${pagecontext.request.contextpath}/bug/ Uploadfile.do ',//server-side request address for file upload fileelementid: ' file1 ',//File upload Space id attribute <input type= "file" id= "file" name = "File"/> type: ' Post ', dataType: ' text ',//return value type is generally set to JSON success:function (data, status)//server Success Response Place function { alert ("File upload succeeded") , error:function (data, status, E)//server response failure handler { alert (" File upload failed ");}} );
Background implementation:
@RequestMapping (value= "/uploadfile.do", produces= "Text/html;charset=utf-8") public @ResponseBody String IMPORTPICFI Le1 (@RequestParam multipartfile file1,httpservletrequest request) {map<string,object& Gt map= new hashmap<string,object> (); if (File1.isempty ()) {map.put ("result", "error"); Map.put ("msg", "Upload file cannot be empty"); } else{String originalfilename=file1.getoriginalfilename (); String Filebasename=filenameutils.getbasename (originalfilename); Date now = new Date (); SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); String flodername=filebasename+ "_" +df.format (now); try{//Create the path to upload file Fdir = new file ("D:/file"); if (!fdir.exists ()) { Fdir.mkdirs (); }//file is uploaded to the path under FileUtils. Copyinputstreamtofile (File1.getinputstream (), New File (Fdir,originalfilename)); Coding Map.put ("result", "success"); } catch (Exception e) {map.put ("result", "error"); Map.put ("msg", E.getmessage ()); } }
2. File download
Front page:
<table><tr><td> accessories </td><td><a href= "" id= "Ssurl" class= "Easyui-linkbutton" > download </a></td></tr></table>
JS Method:
Background method:
/** * File Download * @throws IOException */@RequestMapping (value= "/download", method=requestmethod.get) public void Download (@RequestParam (value= "filename") String filename, httpservletrequest request, Httpservle Tresponse response) throws IOException {//analog file, myfile.txt for files that need to be downloaded String path = "D:\\file" + "\ \" +filename; Gets the input stream InputStream bis = new Bufferedinputstream (new FileInputStream (path)); Transcoding, so that the file name in Chinese garbled filename = urlencoder.encode (filename, "UTF-8"); Set File Download Header Response.AddHeader ("Content-disposition", "attachment;filename=" + filename); 1. Set the file contenttype type, this setting, will automatically determine the download file type Response.setcontenttype ("Multipart/form-data"); Bufferedoutputstream out = new Bufferedoutputstream (Response.getoutputstream ()); int len = 0; while (len = Bis.read ())! =-1) {out.write (len); Out.flush (); } out. Close (); }
My pro-test can be used, we have any ideas to exchange, the first time to write a blog, if there are omissions, please advise!
File upload and file download under the SSM framework