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;
}
}
}