Search in sources :

Example 6 with TempFile

use of org.nutz.mvc.upload.TempFile in project nutz by nutzam.

the class UploadingUnitTest method test_upload_chinese_filename.

/**
     * @author lAndRaxeE(landraxee@gmail.com)
     */
@Test
public void test_upload_chinese_filename() throws UploadException {
    /*
         * 准备模拟对象
         */
    MockHttpServletRequest req = Mock.servlet.request();
    File txt = Files.findFile("org/nutz/mvc/upload/files/quick/中文.txt");
    /*
         * 如果模拟上传时request使用 GBK 编码,用 GBK 来解码,应该会生成正确的文件名
         */
    req.setInputStream(Mock.servlet.insmulti("GBK", txt)).init();
    Uploading up = UploadUnit.TYPE.born();
    TempFile txt2 = (TempFile) up.parse(req, UploadingContext.create(tmps).setCharset("GBK")).get("F0");
    // 测试本地的默认编码是否是GBK,即模拟中文环境,本人环境为中文Windows XP
    // 在JVM参数中增加-Dfile.encoding=GBK即可设置好
    // assertEquals("GBK", Charset.defaultCharset().name());
    //
    // zzh: JUnit 测试必须在多数常用环境下可以比较方便的测试通过,经过这次修改,相信
    // 即可以达到这个目的,又可以测试出中文文件名的编码问题。如果没有其他的问题,在
    // 1.a.30 发布前,这段注释将被删除
    assertEquals("中文.txt", txt2.getSubmittedFileName());
    /*
         * 为了验证上传是否是真的可以解码,再次准备模拟 GBK 的输入流,但是这次将用 UTF-8 来解码
         */
    req.setInputStream(Mock.servlet.insmulti("GBK", txt)).init();
    up = UploadUnit.TYPE.born();
    txt2 = (TempFile) up.parse(req, UploadingContext.create(tmps)).get("F0");
    assertFalse("中文.txt".equals(txt2.getSubmittedFileName()));
}
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) Test(org.junit.Test)

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