2015-09-23 82 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序列被拋出。

嘗試傳遞正確的字符集爲你的情況和這個錯誤不會出現