簡體   English   中英

java.nio.charset.MalformedInputException:輸入長度= 1

[英]java.nio.charset.MalformedInputException: Input length = 1

我有一個zip文件,我需要允許用戶使用支持UTF-8的Java 1.6從部署在Tomcat 6上的JSF應用程序下載。以下是我的jsp頁面上的代碼:

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);
}

從Javadocs:

當輸入字節序列對於給定的字符集不合法,或者輸入字符序列不是合法的16位Unicode序列時,將引發格式錯誤的異常

嘗試根據您的情況傳遞正確的字符集,但不會顯示此錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM