简体   繁体   中英

java.nio.charset.MalformedInputException: Input length = 1

I have a zip file I need to allow users to download from a JSF application deployed on Tomcat 6 using Java 1.6 supporting UTF-8.Following is the code on my jsp page:

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

It used to work when UTF-8 was not supported. Now I keep getting the following exception. Why does the output stream ByteArrayWebOutputStream need to use the decoder? Most suggestions I can find are to handling the encoding for InputStream, but not the OutputStream as indicated by the exception. How can I fix this?

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)

I do use a CharsetFilter as part of the pipeline, but there seems to be no impact on the situation because I removed the URL from the filter targets the result is still the same.

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

From the Javadocs:

Malformed exception is thrown when an input byte sequence is not legal for given charset, or an input character sequence is not a legal sixteen-bit Unicode sequence.

Try passing the correct charset for your case and this error will not show up

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM