Search in sources :

Example 1 with TempFile

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) {
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) TempFile(org.nutz.mvc.upload.TempFile) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) StringReader(java.io.StringReader) HttpServletResponse(javax.servlet.http.HttpServletResponse) ArrayList(java.util.ArrayList) List(java.util.List) UploadException(org.nutz.mvc.upload.UploadException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) NutMap(org.nutz.lang.util.NutMap)

Example 2 with TempFile

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()));
}
Also used : TempFile(org.nutz.mvc.upload.TempFile) MockHttpServletRequest(org.nutz.mock.servlet.MockHttpServletRequest) Uploading(org.nutz.mvc.upload.Uploading) TempFile(org.nutz.mvc.upload.TempFile) File(java.io.File) MultipartInputStream(org.nutz.mock.servlet.multipart.MultipartInputStream) Test(org.junit.Test)

Example 3 with TempFile

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);
}
Also used : TempFile(org.nutz.mvc.upload.TempFile) MockHttpServletRequest(org.nutz.mock.servlet.MockHttpServletRequest) Uploading(org.nutz.mvc.upload.Uploading) TempFile(org.nutz.mvc.upload.TempFile) File(java.io.File) MultipartInputStream(org.nutz.mock.servlet.multipart.MultipartInputStream) Test(org.junit.Test)

Example 4 with TempFile

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()));
}
Also used : TempFile(org.nutz.mvc.upload.TempFile) MockHttpServletRequest(org.nutz.mock.servlet.MockHttpServletRequest) Uploading(org.nutz.mvc.upload.Uploading) TempFile(org.nutz.mvc.upload.TempFile) File(java.io.File) MultipartInputStream(org.nutz.mock.servlet.multipart.MultipartInputStream) Test(org.junit.Test)

Example 5 with TempFile

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);
    }
}
Also used : UploadException(org.nutz.mvc.upload.UploadException) ArrayList(java.util.ArrayList) ServletException(javax.servlet.ServletException) FastUploading(org.nutz.mvc.upload.FastUploading) Entry(java.util.Map.Entry) TempFile(org.nutz.mvc.upload.TempFile) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper)

Aggregations

TempFile (org.nutz.mvc.upload.TempFile)6 File (java.io.File)4 Test (org.junit.Test)4 MockHttpServletRequest (org.nutz.mock.servlet.MockHttpServletRequest)4 Uploading (org.nutz.mvc.upload.Uploading)4 MultipartInputStream (org.nutz.mock.servlet.multipart.MultipartInputStream)3 ArrayList (java.util.ArrayList)2 ServletException (javax.servlet.ServletException)2 HttpServletRequestWrapper (javax.servlet.http.HttpServletRequestWrapper)2 UploadException (org.nutz.mvc.upload.UploadException)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 NutMap (org.nutz.lang.util.NutMap)1 FastUploading (org.nutz.mvc.upload.FastUploading)1