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,&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>
上一篇:我想要得那块牌—记烟台大学第一届"ACM讲堂"


下一篇:asp.net中membership使用oracle数据库(一)