jsp处理表单上传图片(commons-fileupload-1.2.2.jar,commons-io-2.4.jar)

upload.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 3 <html>
 4   <head>
 5     <title>上传图片的表单页面</title>
 6     <meta http-equiv="pragma" content="no-cache">
 7     <meta http-equiv="cache-control" content="no-cache">
 8     <meta http-equiv="expires" content="0">    
 9     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
10     <meta http-equiv="description" content="This is my page">
11   </head>
12   <body>
13     <form action="uploaddo.jsp" method="post" enctype="multipart/form-data">
14         <table>
15             <tr>
16                 <td>姓名:</td>
17                 <td><input type="text" name="txtUserName" /></td>
18             </tr>
19             <tr>
20                 <td>性别:</td>
21                 <td>
22                     <input type="radio" name="rdoSex" value="男" checked="checked"/>23                     <input type="radio" name="rdoSex" value="女"/>24                 </td>
25             </tr>
26             <tr>
27                 <td>教育:</td>
28                 <td>
29                     <select name="selEdu">
30                         <option value="小学">小学</option>
31                         <option value="中学">中学</option>
32                         <option value="大学">大学</option>
33                     </select>
34                 </td>
35             </tr>
36             <tr>
37                 <td>爱好:</td>
38                 <td>
39                     <input type="checkbox" name="rdoHibby" value="篮球"/>篮球
40                     <input type="checkbox" name="rdoHibby" value="足球"/>足球
41                     <input type="checkbox" name="rdoHibby" value="排球"/>排球
42                 </td>
43             </tr>
44             <tr>
45                 <td>图片:</td>
46                 <td><input type="file" name="txtPhoto" /></td>
47             </tr>
48             <tr>
49                 <td colspan="2">
50                     <input type="submit" value="提交"/>
51                 </td>
52             </tr>
53         </table>
54     </form>
55   </body>
56 </html>
View Code

uploaddo.jsp

  1 <%@ page language="java" pageEncoding="UTF-8"%>
  2 <%@ page import="java.util.*,java.io.*,java.lang.*" %>
  3 <%@ page import="org.apache.commons.fileupload.*"%>
  4 <%@ page import="org.apache.commons.fileupload.disk.*"%>
  5 <%@ page import="org.apache.commons.fileupload.servlet.*"%>
  6 <%
  7     HashMap<String,ArrayList<String>> mapField =new HashMap<String,ArrayList<String>>();
  8     HashMap<String,ArrayList<String>> mapFile =new HashMap<String,ArrayList<String>>();
  9     request.setCharacterEncoding("utf-8");
 10     response.setCharacterEncoding("utf-8");
 11     boolean isMultipart = ServletFileUpload.isMultipartContent(request);
 12     if (isMultipart) {
 13         try {
 14             DiskFileItemFactory factory = new DiskFileItemFactory();
 15             factory.setSizeThreshold(1024 * 4);//设置缓冲区4kb
 16             String bufferFilePath = request.getSession().getServletContext().getRealPath("temp/buffer");
 17             factory.setRepository(new File(bufferFilePath));//设置上传文件用到临时文件存放路径
 18             ServletFileUpload upload = new ServletFileUpload(factory);
 19             upload.setFileSizeMax(1024 * 1024 * 3);//设置单个文件的最大限制
 20             List<FileItem> items = upload.parseRequest(request);
 21             Iterator<FileItem> iter = items.iterator();
 22             while (iter.hasNext()) {
 23                 FileItem item = iter.next();
 24                 String fieldName = item.getFieldName();//获取name
 25                 if (item.isFormField()) {
 26                     //如果是表单字段
 27                     String fieldValue = item.getString("utf-8");//获取value
 28                     ArrayList<String> list = null;
 29                     if(mapField.containsKey(fieldName)){
 30                         list = mapField.get(fieldName);
 31                     }else{
 32                         list = new ArrayList<String>();
 33                     }
 34                     list.add(fieldValue);
 35                     mapField.put(fieldName, list);
 36                 } else {
 37                     //如果是上传控件
 38                     String uploadFileName = item.getName();//读取上传图片文件名称
 39                     if (uploadFileName != null && !uploadFileName.equals("")) {
 40                         String uuid = UUID.randomUUID().toString();
 41                         String fileType = uploadFileName.substring(uploadFileName.lastIndexOf("."));
 42                         String fileName = "upload/" + uuid + fileType;
 43                         String uploadFilePath = request.getSession().getServletContext().getRealPath("/");
 44                         File saveFile = new File(uploadFilePath, fileName);
 45                         item.write(saveFile);//保存上传图片到服务器
 46                         ArrayList<String> list = null;
 47                         if(mapFile.containsKey(fieldName)){
 48                             list = mapFile.get(fieldName);
 49                         }else{
 50                             list = new ArrayList<String>();
 51                         }
 52                         list.add(fileName);
 53                         mapFile.put(fieldName, list);
 54                     }
 55                 }
 56             }
 57         } catch (FileUploadBase.FileSizeLimitExceededException ex) {
 58             out.print("上传文件失败,文件超过3M。");
 59         } catch (FileUploadBase.IOFileUploadException ex) {
 60             out.print("设置上传文件用到临时文件存放路径不存在。");
 61         } catch (Exception ex) {
 62             out.print(ex);
 63         }
 64     }
 65 %>
 66 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 67 <html>
 68 <head>
 69     <title>处理上传图片的表单页面</title>
 70     <meta http-equiv="pragma" content="no-cache">
 71     <meta http-equiv="cache-control" content="no-cache">
 72     <meta http-equiv="expires" content="0">
 73     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 74     <meta http-equiv="description" content="This is my page">
 75 </head>
 76 <body>
 77     <%
 78         for(Map.Entry<String,ArrayList<String>> field : mapField.entrySet())
 79         {
 80             String key = field.getKey();
 81             List<String> values = field.getValue();
 82             StringBuffer sb = new StringBuffer();
 83             for(String value : values){
 84                 sb.append(value);
 85                 sb.append(",");
 86             }
 87             String value = sb.toString();
 88             out.print(String.format("name:%s,&nbsp;&nbsp;&nbsp;&nbsp;value:%s<br/>",key,value));
 89         }
 90      %>
 91      <hr/>
 92      <%
 93          for(ArrayList<String> values : mapFile.values())
 94         {
 95             for(String value : values){
 96                 out.print(String.format("<img src=‘%s‘ style=‘width:300px;heigth:300px‘/><br/>",value));
 97             }
 98         }
 99      %>
100 </body>
101 </html>
View Code

 

jsp处理表单上传图片(commons-fileupload-1.2.2.jar,commons-io-2.4.jar),古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。