主要用到的是<input>的多个属性
复制代码
代码如下:
<inputtype=“file”name=“multipleFileUpload”multiple/>
下面是页面的详细代码:
复制代码
代码如下:
<!DOCTYPEhtml><html><head><metahttp-equiv=“Content-Type”content=“text/html;charset=gb2312”/><title>解决方案4-5:发送多个文件</title></head><body><formid=“myForm”action=“http://10.10.25.31:8080/myupload/UploadPhotoServlet”ENCTYPE=“multipart/form-data”METHOD=“POST”><inputtype=“file”name=“multipleFileUpload”multiple/><inputtype=“submit”value=“提交”><inputtype=“reset”value=“重设”></form></body></html>
java后台的详细代码:
复制代码
代码如下:
导入java.io.File;导入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;/***Servlet实现类UploadPhotoServlet*/
公共类UploadPhotoServlet扩展了HttpServlet{privatestaticfinallongserialVersionUID=1L;/***@seeHttpServlet#HttpServlet()*/publicUploadPhotoServlet(){super();//TODO自动生成的构造函数存根}/***@参见HttpServlet#doGet(HttpServletRequest请求,HttpServletResponse响应)*/受保护的voiddoGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{//TODO自动生成的方法存根doPost(request,response);}/***@seeHttpServlet#doPost(HttpServletRequest请求,HttpServletResponse响应)*/
@SuppressWarnings(“unchecked”)受保护的无效doPost(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{StringimagePath=“c://uploadFile//Image//”+getEachDate()+“//”;//按日期生成文件夹FileuploadPath=newFile(imagePath);if(!uploadPath.exists()){uploadPath.mkdirs();}Filetmp=newFile(“c://tmp//”);if(!tmp.exists()){tmp.mkdirs();}DiskFileItemFactoryfactory=newDiskFileItemFactory();//创建磁盘工厂factory.setRepository(tmp);//设置文件缓存路径factory.setSizeThreshold(10*1096);//将文件保存在内存或磁盘临时文件夹中临界值,数值10240,即10kbServletFileUploadsfu=newServletFileUpload(factory);//创建处理工具
sfu.setSizeMax(10*1024*1024);//服务器端可以接收的最大文件大小,-1表示无上限StringfileName=null;尝试{List<FileItem>list=sfu.parseRequest(request);//解析if(list.size()<1){return;}for(intj=0;j<list.size();j++){FileItemitem=list.get(j);fileName=item.getName();if(fileName.equals(“”)){request.getRequestDispatcher(“/com/visualizerPhoto.jsp”)。forward(request,response);返回;}intpos=fileName.lastIndexOf(“。”);//取图片文件格式if(pos>0){Datedate=newDate();fileName=imagePath+date.getTime()+fileName.substring(pos);}System.out.println(“item:”+item);项目。
}}catch(FileUploadExceptione){e.printStackTrace();}catch(Exceptione){e.printStackTrace();}}////15-11-15publicstaticStringgetEachDate(){Calendarcal=Calendar.getInstance();cal.add(Calendar.DATE,0);昨天的字符串=newSimpleDateFormat(“yyyy-MM-dd”).format(cal.getTime());String[]日期=昨天。split(“-”);字符串realDate=dates[0].substring(2,4)+“-”+dates[1]+“-”+dates[2];返回realDate.trim();}}
如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h60595.shtml