JAVA class import org.apache.commons.fileupload.DiskFileUpload; import Org.apache.commons.fileupload.FileItem; @RequestMapping (Value = "Item", params = "Command=upload", method = Requestmethod.) POST) Public String Upload (itemactionformform, httpservletrequest request, HttpServletResponse response) throws Exception { String fullfilename =null; Get Real Absolute path String Realpath = request. GetSession (). Getservletcontext () . Getrealpath ("/images"); System. out. println ("realpath=" +Realpath); Diskfileupload Disk =new diskfileupload (); Try { list<fileitem> Files =(list<fileitem>) disk. Parserequest (request) ; for (FileitemItem : files) { //Parse the parameter if (item. Isformfield ()) { System. out. println ("Item.isformfield ()"); String Field =Item. GetFieldName (); if (field ! =null) { if (field. Equalsignorecase ("ItemNo")) { ItemNo = item. getString (); System. out. println ("itemno=" +ItemNo); } } } //Parse the file data fullfilename =item. GetName (); if (fullfilename = =null) { System. out. println ("Fullfilename not existed."); continue; } Retrive the filename from full pathname. String fileName1 =fullfilename. substring (fullfilename . lastIndexOf ("\ \") + 1); File file1 =new file (realpath,fileName1); file1. Getparentfile (). Mkdirs (); file1. CreateNewFile (); InputStream INS =item. getInputStream (); OutputStream ous =new fileoutputstream (file1); Try { byte []buffer = newbyte[1024]; int len = 0; while (len =ins. Read (buffer)) >-1) OUs. Write (buffer, 0,Len); } finally { OUs. Close (); ins. Close (); } } } Catch (Exceptione) { // TODO auto-generated Catch block e. Printstacktrace (); } return "Redirect:item.do"; }
|