package com.wen.decorator;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
public class MultipartWrapper extends HttpServletRequestWrapper {
private Map<String, String[]> params = new HashMap<String, String[]>();
// private final static String PATH =
// "C:\\Users\\John_wen\\Workspaces\\MyEclipse 10\\FileUpload\\WebRoot\\upload\\";
public MultipartWrapper(HttpServletRequest request) throws FileUploadException, IOException {
super(request);
setParams(request);
}
private void setParams(HttpServletRequest request) throws FileUploadException, IOException {
boolean isMul = ServletFileUpload.isMultipartContent(request);
if (isMul) {// 如果为true表示是文件上传
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator fileItemIterator = upload.getItemIterator(request);
InputStream is = null;
while (fileItemIterator.hasNext()) {
FileItemStream fis = fileItemIterator.next();
is = fis.openStream();
if (fis.isFormField()) {
setFormField(fis.getFieldName(), is);
} else {
if (fis.getName() != null && !"".equals(fis.getName())) {
String path = request.getSession().getServletContext().getRealPath("/upload");
// 表示文件,上传操作
Streams.copy(is, new FileOutputStream(path + "\\" + fis.getName()), true);
}
params.put(fis.getFieldName(), new String[] { fis.getName() });
}
}
} else {// 正常流程
params = request.getParameterMap();
}
}
private void setFormField(String fieldName, InputStream is) throws IOException {
// 先取出params中对应的fieldName的值;
String[] values = params.get(fieldName);
if (params.containsKey(fieldName)) {
// 如果包含有key
values = Arrays.copyOf(values, values.length + 1);
values[values.length - 1] = Streams.asString(is);
params.put(fieldName, values);
} else {
params.put(fieldName, new String[] { Streams.asString(is) });
}
}
@Override
public String getParameter(String name) {
String[] values = params.get(name);
if (values != null) {
return values[0];
} else {
return null;
}
}
@Override
public Map<String, String[]> getParameterMap() {
return params;
}
@Override
public String[] getParameterValues(String name) {
String[] values = params.get(name);
if (values != null) {
return values;
} else {
return null;
}
}
}
永远的幸福a
- 粉丝: 24
- 资源: 14
最新资源
- Java 多线程课程的代码及少量注释.zip
- 数据库课程设计-基于的个性化购物平台的建表语句.sql
- 数据库课程设计-基于的图书智能一体化管理系统的建表语句.sql
- Java 代码覆盖率库.zip
- Java 代码和算法的存储库 也为该存储库加注星标 .zip
- 免安装Windows10/Windows11系统截图工具,无需安装第三方截图工具 双击直接使用截图即可 是一款免费可靠的截图小工具哦~
- Libero Soc v11.9的安装以及证书的获取(2021新版).zip
- BouncyCastle.Cryptography.dll
- 5.1 孤立奇点(JD).ppt
- 基于51单片机的智能交通灯控制系统的设计与实现源码+报告(高分项目)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈