使用Apache fileUpload库上传文件(支持多个文件)
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> |
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。