The main thing used is the multiple attribute of
The following is the detailed code of the page:
Solution 4-5: Sending multiple files < /head> action="http://10.10.25.31:8080/myupload/UploadPhotoServlet" ENCTYPE="multipart/form-data" METHOD="POST"> type="submit" value="Submit">
Detailed code of java background:
java.io.파일 가져오기; java.io.IOException 가져오기; java.text.SimpleDateFormat 가져오기; java.util.Calendar 가져오기; java.util.Date 가져오기; java.util.List 가져오기; javax.servlet.ServletException 가져오기; javax.servlet.http.HttpServlet 가져오기; javax.servlet.http.HttpServletRequest 가져오기; javax.servlet.http.HttpServletResponse 가져오기; org.apache.commons.fileupload.FileItem 가져오기; 가져오기 org.apache.commons.fileupload.FileUploadException; org.apache.commons.fileupload.disk.DiskFileItemFactory 가져오기; org.apache.commons.fileupload.servlet.ServletFileUpload 가져오기; /** * 서블릿 구현 클래스 UploadPhotoServlet */ public class UploadPhotoServlet은 HttpServlet을 확장합니다. { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public UploadPhotoServlet() { super(); // TODO 자동 생성 생성자 스텁 } /** * @see HttpServlet#doGet(HttpServletRequest 요청, HttpServletResponse 응답) */ protected void doGet(HttpServletRequest 요청, HttpServletResponse 응답) throws ServletException, IOException { // TODO 자동 생성된 메소드 스텁 doPost(request,response); } /** * @see HttpServlet#doPost(HttpServletRequest 요청, HttpServletResponse 응답) */ @SuppressWarnings("unchecked") protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String imagePath=" c:\uploadFile\Image\" getEachDate() "\";//按日期生成文件夹 File uploadPath=new File(imagePath); if(!uploadPath.exists()){ uploadPath.mkdirs(); } 파일 tmp=new File("c:\tmp\"); if(!tmp.exists()){ tmp.mkdirs(); } DiskFileItemFactory Factory=new DiskFileItemFactory ();//创建磁盘工厂 factory.setRepository(tmp);//设置文件缓存路径 factory.setSizeThreshold(10 * 1096 );//将文件保存는 内存还是磁盘临时文件夹的默认临界值,值为10240,即10kb ServletFileUpload sfu=new ServletFileUpload(factory);//创建处理工具 sfu.setSizeMax(10*1024*1024 );//더 보기 { List list=sfu.parseRequest(request);//설명 if(list.size()<1){ return; } for(int j=0;j FileItem item=list.get(j); fileName=item.getName(); if(fileName.equals("")){ request.getRequestDispatcher("/com/visualizerPhoto.jsp").forward(요청, 응답); 반환; } int pos=fileName.lastIndexOf(".");//取图文件格式 if(pos>0){ 날짜 date=new Date(); fileName=imagePath date.getTime() fileName.substring(pos); } System.out.println("item:" item); item.write(new File(fileName));//写到磁盘 } } catch (FileUploadException e) { e.printStackTrace(); } catch(예외 e) { e.printStackTrace(); } } // 13-11-15 public static String getEachDate() { Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, 0); 어제 문자열 = new SimpleDateFormat("yyyy-MM-dd ").format(cal .getTime()); String[] 날짜 = 어제.split("-"); String realDate = 날짜[0].substring(2, 4) "-" 날짜[1] "-" 날짜[2]; realDate.trim()을 반환합니다. } }
하면是效果图: