Spring 3.0验证通过
import org.apache.commons.fileupload.DiskFileUpload; import org.apache.commons.fileupload.FileItem; public String upload(HttpServletRequest request, HttpServletResponse response) throws Exception { String fullFileName = null; DiskFileUpload disk = new DiskFileUpload(); try { List<FileItem> files = (List<FileItem>) disk.parseRequest(request); for (FileItem item : files) { fullFileName = item.getName(); if(fullFileName == null) { System.out.println("fullFileName not existed."); continue; } 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 = new byte[1024]; int len = 0; while((len = ins.read(buffer)) > -1) ous.write(buffer,0,len); }finally{ ous.close(); ins.close(); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return "redirect:item.do"; } |
<form action="item.do " enctype="multipart/form-data" method="post"> <input name="itemFile" type="file" class="text1" size="40" maxlength="40">(必须是.gif或.jpg文件) </form> |