upload.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>上传图片的表单页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<form action="uploaddo.jsp" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="txtUserName" /></td>
</tr>
<tr>
<td>性别:</td>
<td>
<input type="radio" name="rdoSex" value="男" checked="checked"/>男
<input type="radio" name="rdoSex" value="女"/>女
</td>
</tr>
<tr>
<td>教育:</td>
<td>
<select name="selEdu">
<option value="小学">小学</option>
<option value="中学">中学</option>
<option value="大学">大学</option>
</select>
</td>
</tr>
<tr>
<td>爱好:</td>
<td>
<input type="checkbox" name="rdoHibby" value="篮球"/>篮球
<input type="checkbox" name="rdoHibby" value="足球"/>足球
<input type="checkbox" name="rdoHibby" value="排球"/>排球
</td>
</tr>
<tr>
<td>图片:</td>
<td><input type="file" name="txtPhoto" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交"/>
</td>
</tr>
</table>
</form>
</body>
</html>

uploaddo.jsp

 <%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="java.util.*,java.io.*,java.lang.*" %>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%
HashMap<String,ArrayList<String>> mapField =new HashMap<String,ArrayList<String>>();
HashMap<String,ArrayList<String>> mapFile =new HashMap<String,ArrayList<String>>();
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(1024 * 4);//设置缓冲区4kb
String bufferFilePath = request.getSession().getServletContext().getRealPath("temp/buffer");
factory.setRepository(new File(bufferFilePath));//设置上传文件用到临时文件存放路径
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(1024 * 1024 * 3);//设置单个文件的最大限制
List<FileItem> items = upload.parseRequest(request);
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = iter.next();
String fieldName = item.getFieldName();//获取name
if (item.isFormField()) {
//如果是表单字段
String fieldValue = item.getString("utf-8");//获取value
ArrayList<String> list = null;
if(mapField.containsKey(fieldName)){
list = mapField.get(fieldName);
}else{
list = new ArrayList<String>();
}
list.add(fieldValue);
mapField.put(fieldName, list);
} else {
//如果是上传控件
String uploadFileName = item.getName();//读取上传图片文件名称
if (uploadFileName != null && !uploadFileName.equals("")) {
String uuid = UUID.randomUUID().toString();
String fileType = uploadFileName.substring(uploadFileName.lastIndexOf("."));
String fileName = "upload/" + uuid + fileType;
String uploadFilePath = request.getSession().getServletContext().getRealPath("/");
File saveFile = new File(uploadFilePath, fileName);
item.write(saveFile);//保存上传图片到服务器
ArrayList<String> list = null;
if(mapFile.containsKey(fieldName)){
list = mapFile.get(fieldName);
}else{
list = new ArrayList<String>();
}
list.add(fileName);
mapFile.put(fieldName, list);
}
}
}
} catch (FileUploadBase.FileSizeLimitExceededException ex) {
out.print("上传文件失败,文件超过3M。");
} catch (FileUploadBase.IOFileUploadException ex) {
out.print("设置上传文件用到临时文件存放路径不存在。");
} catch (Exception ex) {
out.print(ex);
}
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>处理上传图片的表单页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<%
for(Map.Entry<String,ArrayList<String>> field : mapField.entrySet())
{
String key = field.getKey();
List<String> values = field.getValue();
StringBuffer sb = new StringBuffer();
for(String value : values){
sb.append(value);
sb.append(",");
}
String value = sb.toString();
out.print(String.format("name:%s,&nbsp;&nbsp;&nbsp;&nbsp;value:%s<br/>",key,value));
}
%>
<hr/>
<%
for(ArrayList<String> values : mapFile.values())
{
for(String value : values){
out.print(String.format("<img src='%s' style='width:300px;heigth:300px'/><br/>",value));
}
}
%>
</body>
</html>

jsp处理表单上传图片(commons-fileupload-1.2.2.jar,commons-io-2.4.jar)的更多相关文章

  1. wangEditor ie9 表单上传图片

    wangEditor ie9 表单上传图片  弹框无法消失  var resultText = $.trim(iframeWindow.document.body.innerHTML); result ...

  2. JSP的表单处理

    以下内容引用自http://wiki.jikexueyuan.com/project/jsp/form-processing.html: 当需要从浏览器向Web服务器传递一些信息并最终将信息返回到后端 ...

  3. 在jsp提交表单的参数封装到一个方法里

    建议去看一下孤傲苍狼写的Servlet+JSP+JavaBean开发模式(http://www.cnblogs.com/xdp-gacl/p/3902537.html), 最好把他JavaWeb学习总 ...

  4. JSP 用户表单的简单实现

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...

  5. JSP将表单提交并在本页中显示

    代码如下: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8& ...

  6. jsp注册页面验证,easyui的jsp+js表单验证

    1.1下面的代码是写在Js里面的,就直接写进去不用什么其他东西,这样一个表单验证就好了(1.2图) $.extend($.fn.validatebox.defaults.rules, { phone: ...

  7. 摒弃FORM表单上传图片,异步批量上传照片

    之前作图像处理一直在用form表单做图片数据传输, 个人感觉low到爆炸而且用户体验极差,现在介绍一个一部批量上传图片的小技巧,忘帮助他人的同时也警醒自己在代码的编写时不要只顾着方便,也要考虑代码的健 ...

  8. Ajax在jQuery中的应用 (4)向jsp提交表单数据

    ajax技术带给我们的是良好的用户体验,同时,使用jquery可以简化开发,提高工作效率. 下面就介绍一下大致的开发步骤. 工具/原料 本文中使用的是 jquery-1.3.2.min.js 方法/步 ...

  9. 【转】JSP提交表单

    设计表单页面,它是静态页面,使用HTML编写,而且使用了JavaScript脚本语言来验证填写表单数据,表单页面为form.htm,代码如下: <html><head>< ...

随机推荐

  1. @ArrayList剖析

    第1部分 ArrayList介绍 ArrayList简介 Resizable-array implementation of the List interface. Implements all op ...

  2. OBjective-C:在可变数组NSMutableArray中添加相同对象后,进行自定义的排序方式输出

    以下为自定义的排序方式的实现 #import "Person+Compare.h" @implementation Person (Compare) -(NSComparisonR ...

  3. 网站流量分析指标-PV/UV/PR/IP

    网站数据分析,经常会统计一个页面或者一个网站或者其他情况的PV/UV.下面简单说一下,这些量PV/UV/PR/IP. 1.PV PV(page view),即页面浏览量,或点击量.通常是衡量一个网络新 ...

  4. ubuntu/wireshark: There are no interfaces on which a capture can be done.故障解决

    [转载]http://blog.csdn.net/ccwwff/article/details/6697258 在ubuntu安装wireshark, 在启动程序启动wireshark. 点captr ...

  5. 依赖注入 DI 控制反转 IOC MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  6. spring cloud 报错Error creating bean with name 'hystrixCommandAspect' ,解决方案

    spring cloud 升级到最新版 后,报错: org.springframework.beans.factory.BeanCreationException: Error creating be ...

  7. Oracle基础重点概要

    表空间                                                                 逻辑上处于数据库之下,利用表空间可以更灵活地规划数据库结构. 创 ...

  8. githug-54-git练习

    1-40: http://wiki.jikexueyuan.com/project/git-54-stage-clear/ 41-50: https://blog.csdn.net/maxam0128 ...

  9. Spring+DBUnit+H2----项目单元测试

    http://yugouai.iteye.com/blog/1879337 今天够郁闷的,早上调好的代码,到中午调试不同了,分析不出问题,H2的JDBC报错:org.h2.jdbc.JdbcSQLEx ...

  10. Camera2必知必会

    引言 一切源于在项目过程中的一个Bug:我的需求是在MainActivity 实现自动预览,也可以点击跳到签到SignedActivity去实现拍照签到,第一次进入界面的时候都是正常的,但是有时候返回 ...