jsp处理表单上传图片(commons-fileupload-1.2.2.jar,commons-io-2.4.jar)
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, 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)的更多相关文章
- wangEditor ie9 表单上传图片
wangEditor ie9 表单上传图片 弹框无法消失 var resultText = $.trim(iframeWindow.document.body.innerHTML); result ...
- JSP的表单处理
以下内容引用自http://wiki.jikexueyuan.com/project/jsp/form-processing.html: 当需要从浏览器向Web服务器传递一些信息并最终将信息返回到后端 ...
- 在jsp提交表单的参数封装到一个方法里
建议去看一下孤傲苍狼写的Servlet+JSP+JavaBean开发模式(http://www.cnblogs.com/xdp-gacl/p/3902537.html), 最好把他JavaWeb学习总 ...
- JSP 用户表单的简单实现
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...
- JSP将表单提交并在本页中显示
代码如下: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8& ...
- jsp注册页面验证,easyui的jsp+js表单验证
1.1下面的代码是写在Js里面的,就直接写进去不用什么其他东西,这样一个表单验证就好了(1.2图) $.extend($.fn.validatebox.defaults.rules, { phone: ...
- 摒弃FORM表单上传图片,异步批量上传照片
之前作图像处理一直在用form表单做图片数据传输, 个人感觉low到爆炸而且用户体验极差,现在介绍一个一部批量上传图片的小技巧,忘帮助他人的同时也警醒自己在代码的编写时不要只顾着方便,也要考虑代码的健 ...
- Ajax在jQuery中的应用 (4)向jsp提交表单数据
ajax技术带给我们的是良好的用户体验,同时,使用jquery可以简化开发,提高工作效率. 下面就介绍一下大致的开发步骤. 工具/原料 本文中使用的是 jquery-1.3.2.min.js 方法/步 ...
- 【转】JSP提交表单
设计表单页面,它是静态页面,使用HTML编写,而且使用了JavaScript脚本语言来验证填写表单数据,表单页面为form.htm,代码如下: <html><head>< ...
随机推荐
- 系统对接API调用
在与公司外部系统对接时,API接口一般采用REST风格,对外暴露HTTP服务.只需要将入参封装好,并发起HTTP请求即可.具体请求流程如下图所示: 数据格式 API调用参数分为系统参数和业务参数,请求 ...
- OpenCV学习(27) 直方图(4)
我们可以利用OpenCV的直方图,backproject直方图和meanshift算法来跟踪物体.下面通过简单的例子来说明如何实现跟踪算法,我们有两幅狒狒的图片,如下图所示:我们首先在左图中框选狒狒的 ...
- longest-substring-with-at-least-k-repeating-characters
https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/ public class S ...
- Unity的延迟管线
unity buildin deferred pipeline rt0 albedo rt1 spec rt2 normal rt3 emissive rt4 shadowmask rt3的使用方式 ...
- Longest Common Prefix leetcode java
题目: Write a function to find the longest common prefix string amongst an array of strings. 题解: 解题思路是 ...
- AngularJs HTTP响应拦截器实现登陆、权限校验
$httpAngularJS 的 $http 服务允许我们通过发送 HTTP 请求方式与后台进行通信.在某些情况下,我们希望可以俘获所有的请求,并且在将其发送到服务端之前进行操作.还有一些情况是,我们 ...
- C++11中万能的可调用类型声明std::function<...>
在C++11中,callable object 包括传统C函数,C++成员函数,函数对象(实现了()运算符的类的实例),lambda表达式(特殊函数对象)共4种.程序设计,特别是程序库设计时,经常需要 ...
- 避免闪烁的方法(OnEraseBkgnd)
在图形图象处理编程过程中,双缓冲是一种主要的技术.我们知道,假设窗口在响应WM_PAINT消息的时候要进行复杂的图形处理,那么窗口在重绘时因为过频的刷新而引起闪烁现象. 解决这一问题的有效方法就是双缓 ...
- linux下apache的使用
Linux安装配置apache http://www.cnblogs.com/fly1988happy/archive/2011/12/14/2288064.html 1.获取软件: http://h ...
- 在MyEclipse中设置jsp页面为默认utf-8编码(转)
http://www.cnblogs.com/xdp-gacl/p/3496161.html 在MyEclispe中创建Jsp页面,Jsp页面的默认编码是“ISO-8859-1”,如下图所示: 在这种 ...