解决jsp打开word文档乱码问题
2009-04-21 17:50
435 查看
用jsp打开word,写法如下:
点击链接调用该页面,弹出提示窗,打开or下载,选打开,然后word并没有打开文档,而是要求选择编码方式,windows默认 或者其他编码,但经过试验,基本都不能正常打开,而是打开了一堆乱码。
我这里,问题出在这句话:response.setHeader("Content-disposition","inline; filename=mc.doc");
将里面的"inline" 换成:attachment,
即: response.setHeader("Content-disposition","attachment; filename=mc.doc");
问题解决,不会出现编码选择的提示框,正常打开word。
%@page contentType="application/msword;charset=utf8"%> <%@page import="java.io.*"%> <% String filename = ""; if (request.getParameter("docfilename") != null) { filename = request.getParameter("docfilename"); } File f = new File(filename); if(!f.exists() || f.isDirectory()){ System.out.println(">>>>>>>>>>>>>>>>>>>>>>>文件不存在"); out.println("<mce:script language=/"JavaScript/"><!-- "); out.println("alert(/"文件未找到/");"); out.println("window.close();"); out.println(" // --></mce:script>"); out.close(); return; } response.setContentType("application/msword"); response.setHeader("Content-disposition","inline; filename=mc.doc"); BufferedInputStream bis = null; BufferedOutputStream bos = null; try { //bis = new BufferedInputStream(new FileInputStream(getServletContext().getRealPath("" + filename))); bis = new BufferedInputStream(new FileInputStream(filename)); bos = new BufferedOutputStream(response.getOutputStream()); byte[] buff = new byte[2048]; int bytesRead; while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff,0,bytesRead); } } catch(FileNotFoundException fe){ System.out.println("文件没找到"); } catch(final IOException e) { System.out.println ( "出现IOException." + e ); } finally { if (bis != null) bis.close(); if (bos != null) bos.close(); out.clear(); out = pageContext.pushBody(); } return; %>
点击链接调用该页面,弹出提示窗,打开or下载,选打开,然后word并没有打开文档,而是要求选择编码方式,windows默认 或者其他编码,但经过试验,基本都不能正常打开,而是打开了一堆乱码。
我这里,问题出在这句话:response.setHeader("Content-disposition","inline; filename=mc.doc");
将里面的"inline" 换成:attachment,
即: response.setHeader("Content-disposition","attachment; filename=mc.doc");
问题解决,不会出现编码选择的提示框,正常打开word。
相关文章推荐
- #Mac技巧#如何在Mac系统上新建TXT文档,以及打开txt文稿的乱码问题如何解决
- 用jsp动态输出excel文档和中文乱码问题的解决
- 解决PHP文件(word/excel)下载 内容打开乱码问题
- 在weblogic下如何解决Word、Excel打开乱码问题
- 用jsp动态输出excel文档和中文乱码问题的解决
- 解决VS2005打开UTF-8编码文档乱码问题(ZZ)
- 如何解决IE打开Word、Excel乱码问题
- [Ubuntu--VIM]vim打开代码文档中文注释显示为乱码的问题解决
- 解决centos6.4打开windows下txt文档显示乱码的问题
- jSP程序页面中下载打开Word,Excel,powerpoint文件乱码的问题
- 解决Ubuntu中打开Windows下的文本文档乱码问题
- ubuntu下解决gedit 打开windows 下的. txt 文档,乱码问题
- sublime text3打开中文文档乱码问题解决
- 用jsp动态输出excel文档和中文乱码问题的解决
- 通过freemarker生成一个word,解决生成的word用wps打开有问题的问题,解决出word时中文文件名乱码问题,解决打开出word时打开的word出现问题的问题,出图片,解决动态列表
- linux 解决man命令输出到文档打开乱码问题
- 解决IE不弹出下载框而直接打开Word、Excel乱码问题
- 用jsp动态输出excel文档和中文乱码问题的解决
- 用jsp动态输出excel文档和中文乱码问题的解决