我有一个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);
}
答案 0 :(得分:0)
来自Javadocs:
当输入字节序列不是时,抛出Malformed exception 给定字符集合法,或输入字符序列不合法 十六位Unicode序列。
尝试为您的案例传递正确的字符集,此错误不会显示