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>< ...
随机推荐
- spark调优经验(待续)
spark调优是须要依据业务须要调整的,并非说某个设置是一成不变的,就比方机器学习一样,是在不断的调试中找出当前业务下更优的调优配置.以下零碎的总结了一些我的调优笔记. spark 存储的时候存在严重 ...
- C++ string 字符串查找匹配
在写C++程序中,总会遇到要从一个字符串中查找一小段子字符串的情况,对于在C中,我们经常用到strstr()或者strchr()这两种方法.而对于C++的string,我们往往会用到find(). C ...
- Pascal VOC & COCO数据集介绍 & 转换
目录 Pascal VOC & COCO数据集介绍 Pascal VOC数据集介绍 1. JPEGImages 2. Annotations 3. ImageSets 4. Segmentat ...
- android 下的网络图片加载
Android图片的异步加载,主要原理: 加载图片时先查看缓存中时候存在该图片,如果存在则返回该图片,否则先加载载一个默认的占位图片,同时创建一个通过网络获取图片的任务并添加,任务完成后放松消息给主线 ...
- Realm Swift
Realm Swift 当前这个翻译,主要是方便我自己查阅api,有非常多地方写的比較晦涩或者没有翻译,敬请谅解 version 0.98.7 官方文档 參考文献 Realm支持类型 String,N ...
- nodejs自动热加载文件的做法
https://stackoverflow.com/questions/1972242/how-to-auto-reload-files-in-node-js -------------------- ...
- Cognos值提示设置小技巧
针对值提示问题做一个小的总结: 1:显示类问题 如上图,如何让”英文参数名"和"分割线----"不显示,或者说指定中文显示值呢 (1):让”英文参数名"和&qu ...
- FM同步数据库中结构已经发生变化的表
接触Cognos很久了,最近遇到一个小问题. 在FM模型设计的过程中,有一张表jd_f_order.之后为了更全面的分析这个数据,在这个事实表中引入了一个新的字段商品类型字段,结构如图 但是由于jd_ ...
- XGBoost:在Python中使用XGBoost
原文:http://blog.csdn.net/zc02051126/article/details/46771793 在Python中使用XGBoost 下面将介绍XGBoost的Python模块, ...
- [Backbone]6. Collections.
Define a collection: var AppointmentList = Backbone.Collection.extend({model: Appointment}); RESET t ...