2015-09-23 81 views
0

我有一个zip文件,我需要允许用户从使用支持UTF-8的Java 1.6的Tomcat 6上部署的JSF应用程序进行下载。以下是我的jsp页面:java.nio.charset.MalformedInputException:输入长度= 1

File downloadFile = new File(filePath+ "/" +zipfileName) 
try { 
    response.setContentType("APPLICATION/OCTET-STREAM"); 
    response.setHeader("Content-Disposition","attachment; filename=\""+ +zipfileName;+ "\""); 

    //stream out file 
    FileInputStream fInputStream =new FileInputStream(downloadFile); 
    ServletOutputStream fOutputStream = response.getOutputStream(); 
    int i; 
    while ((i=fInputStream.read()) != -1) { 
     fOutputStream.write(i); 
    } 
    fInputStream.close(); 
    fOutputStream.close(); 
} catch (Exception exp){ 
    System.out.println("Exception: "+exp.getMessage()); 
} 

它在UTF-8不支持时用于工作。现在我不断收到以下异常。为什么输出流ByteArrayWebOutputStream需要使用解码器?我能找到的大部分建议是处理InputStream的编码,但不处理异常所示的OutputStream。我怎样才能解决这个问题?

java.nio.charset.MalformedInputException: Input length = 1 
at java.nio.charset.CoderResult.throwException(CoderResult.java:277) 
at java.nio.charset.CharsetDecoder.decode(CharsetDecoder.java:798) 
at com.sun.faces.application.ByteArrayWebOutputStream.writeTo(ByteArrayWebOutputStream.java:112) 
at com.sun.faces.application.ViewHandlerResponseWrapper.flushToWriter(ViewHandlerResponseWrapper.java:162) 
at com.sun.faces.application.view.JspViewHandlingStrategy.renderView(JspViewHandlingStrategy.java:240) 
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:126) 
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:127) 
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139) 
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:313) 

我使用一个CharsetFilter作为管道的一部分,但似乎因为我移除了过滤器的目标,结果还是一样的URL是对形势没有任何影响。

public void doFilter(ServletRequest request, ServletResponse response, FilterChain next) 
throws IOException, ServletException 
{ 
    // Respect the client-specified character encoding 
    // (see HTTP specification section 3.4.1) 
    if(null == request.getCharacterEncoding()) 
     request.setCharacterEncoding(encoding); 

    /** 
    * Set the default response content type and encoding 
    */ 
    response.setContentType("text/html; charset=UTF-8"); 
    response.setCharacterEncoding("UTF-8"); 

    next.doFilter(request, response); 
} 
+0

你使用任何特定的字符集? – Rehman

+0

不,我有几个xls和txt文件压缩在一起作为测试。但我的网站启用了UTF-8,即每个页面支持utf-8,Web应用程序的所有方面都可以处理utf-8等。我确实有一个Charset过滤器。 – jCoder

回答

0

从的Javadoc:

Malformed exception当输入字节序列是不合法 给定字符集,或者输入字符序列不是合法的 16位Unicode序列被抛出。

尝试传递正确的字符集为你的情况和这个错误不会出现