use of org.nutz.mvc.upload.TempFile in project nutz by nutzam.
the class WhaleFilter method doFilter.
@SuppressWarnings("unchecked")
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
final HttpServletRequest req = (HttpServletRequest) request;
final HttpServletResponse resp = (HttpServletResponse) response;
// 设置req的编码
if (inputEnc != null)
req.setCharacterEncoding(inputEnc);
// 设置resp的编码
if (outputEnc != null)
resp.setCharacterEncoding(outputEnc);
// 如果是POST请求,有很多可以hack的东西
if ("POST".equals(req.getMethod())) {
// 处理隐藏HTTP METHOD, _method参数模式
if (methodParam != null) {
String qs = req.getQueryString();
if (qs != null && qs.contains("_method=")) {
final NutMap map = Mvcs.toParamMap(new StringReader(qs), inputEnc == null ? Charset.defaultCharset().name() : inputEnc);
request = new HttpServletRequestWrapper(req) {
public String getMethod() {
return map.getString(methodParam);
}
};
}
} else // 处理 X-HTTP-Method-Override
if (allowHTTPMethodOverride && req.getHeader("X-HTTP-Method-Override") != null) {
request = new HttpServletRequestWrapper(req) {
public String getMethod() {
return req.getHeader("X-HTTP-Method-Override");
}
};
}
// 处理文件上传
String contentType = req.getContentType();
if (contentType != null) {
if (uc != null && contentType.contains("multipart/form-data")) {
request = handleUpload(req);
}
}
}
try {
chain.doFilter(request, response);
} finally {
try {
List<TempFile> files = (List<TempFile>) req.getAttribute("_files");
if (files != null) {
for (TempFile tf : files) {
if (tf != null)
tf.delete();
}
}
} catch (Exception e) {
}
}
}
use of org.nutz.mvc.upload.TempFile in project nutz by nutzam.
the class UploadingUnitTest method test_upload_empty_just_r_n.
/**
* for issue #617
*/
@Test
public void test_upload_empty_just_r_n() throws Exception {
MockHttpServletRequest req = Mock.servlet.request();
req.setPathInfo("/nutz/junit/uploading");
MultipartInputStream ins = Mock.servlet.insmulti(charset);
File f = Files.findFile("org/nutz/mvc/upload/files/_r_n.txt");
ins.append("theF", f);
req.setInputStream(ins);
req.init();
/*
* 默认不忽略空文件
*/
Uploading up = UploadUnit.TYPE.born();
Map<String, Object> map = up.parse(req, UploadingContext.create(tmps));
assertEquals(1, map.size());
TempFile tf = (TempFile) map.get("theF");
assertEquals("_r_n.txt", tf.getSubmittedFileName());
assertTrue(Streams.equals(Streams.fileIn(f), tf.getInputStream()));
}
use of org.nutz.mvc.upload.TempFile in project nutz by nutzam.
the class UploadingUnitTest method test_upload_ignore_null.
/**
* 测试忽略空文件
*/
@Test
public void test_upload_ignore_null() throws Exception {
MockHttpServletRequest req = Mock.servlet.request();
req.setPathInfo("/nutz/junit/uploading");
File txt = Files.findFile("org/nutz/mvc/upload/files/quick/abc.zdoc");
File empty = Files.findFile("org/nutz/mvc/upload/files/empty.txt");
MultipartInputStream ins = Mock.servlet.insmulti(charset);
ins.append("abc", txt);
ins.append("empty", empty);
req.setInputStream(ins);
req.init();
/*
* 默认不忽略空文件
*/
Uploading up = UploadUnit.TYPE.born();
Map<String, Object> map = up.parse(req, UploadingContext.create(tmps));
assertEquals(2, map.size());
TempFile txt2 = (TempFile) map.get("abc");
TempFile empty2 = (TempFile) map.get("empty");
assertEquals("abc.zdoc", txt2.getSubmittedFileName());
assertTrue(Streams.equals(Streams.fileIn(txt), txt2.getInputStream()));
assertEquals("empty.txt", empty2.getSubmittedFileName());
assertTrue(Streams.equals(Streams.fileIn(empty), empty2.getInputStream()));
/*
* 设置忽略空文件
*/
ins = Mock.servlet.insmulti(charset);
ins.append("abc", txt);
ins.append("empty", empty);
req.setInputStream(ins);
req.init();
up = UploadUnit.TYPE.born();
map = up.parse(req, UploadingContext.create(tmps).setIgnoreNull(true));
assertEquals(1, map.size());
txt2 = (TempFile) map.get("abc");
empty2 = (TempFile) map.get("empty");
assertEquals("abc.zdoc", txt2.getSubmittedFileName());
assertTrue(Streams.equals(Streams.fileIn(txt), txt2.getInputStream()));
assertNull(empty2);
}
use of org.nutz.mvc.upload.TempFile in project nutz by nutzam.
the class UploadingUnitTest method test_upload_1txt_3img.
@Test
public void test_upload_1txt_3img() throws Exception {
MockHttpServletRequest req = Mock.servlet.request();
req.setPathInfo("/nutz/junit/uploading");
File txt = Files.findFile("org/nutz/mvc/upload/files/quick/abc.zdoc");
File red = Files.findFile("org/nutz/mvc/upload/files/quick/red.png");
File blue = Files.findFile("org/nutz/mvc/upload/files/quick/blue.png");
File green = Files.findFile("org/nutz/mvc/upload/files/quick/green.png");
MultipartInputStream ins = Mock.servlet.insmulti(charset);
ins.append("abc", txt);
ins.append("red", red);
ins.append("blue", blue);
ins.append("green", green);
req.setInputStream(ins);
req.init();
Uploading up = UploadUnit.TYPE.born();
Map<String, Object> map = up.parse(req, UploadingContext.create(tmps));
assertEquals(4, map.size());
TempFile txt2 = (TempFile) map.get("abc");
TempFile red2 = (TempFile) map.get("red");
TempFile blue2 = (TempFile) map.get("blue");
TempFile green2 = (TempFile) map.get("green");
assertEquals("abc.zdoc", txt2.getSubmittedFileName());
assertTrue(Streams.equals(Streams.fileIn(txt), txt2.getInputStream()));
assertEquals("red.png", red2.getSubmittedFileName());
assertTrue(Streams.equals(Streams.fileIn(red), red2.getInputStream()));
assertEquals("blue.png", blue2.getSubmittedFileName());
assertTrue(Streams.equals(Streams.fileIn(blue), blue2.getInputStream()));
assertEquals("green.png", green2.getSubmittedFileName());
assertTrue(Streams.equals(Streams.fileIn(green), green2.getInputStream()));
}
use of org.nutz.mvc.upload.TempFile in project nutz by nutzam.
the class WhaleFilter method handleUpload.
@SuppressWarnings("unchecked")
public HttpServletRequest handleUpload(HttpServletRequest req) throws ServletException {
try {
FastUploading fup = new FastUploading();
final Map<String, Object> params = fup.parse(req, (UploadingContext) uc);
final List<TempFile> files = new ArrayList<TempFile>();
Iterator<Entry<String, Object>> it = params.entrySet().iterator();
while (it.hasNext()) {
Object obj = it.next().getValue();
final boolean[] re = new boolean[1];
Lang.each(obj, new Each<Object>() {
public void invoke(int index, Object ele, int length) {
if (ele != null && ele instanceof TempFile) {
files.add((TempFile) ele);
re[0] = true;
}
}
});
if (re[0])
it.remove();
}
req.setAttribute("_files", files);
params.putAll(req.getParameterMap());
return new HttpServletRequestWrapper(req) {
public String getParameter(String name) {
return (String) params.get(name);
}
@SuppressWarnings("rawtypes")
public Map getParameterMap() {
return params;
}
@SuppressWarnings("rawtypes")
public Enumeration getParameterNames() {
return Collections.enumeration(params.keySet());
}
public String[] getParameterValues(String name) {
if (params.containsKey(name))
return new String[] { (String) params.get(name) };
return null;
}
};
} catch (UploadException e) {
throw new ServletException("upload fail", e);
}
}
Aggregations