/** * @param byteArrayOutputStream 将文件内容写入ByteArrayOutputStream * @param response HttpServletResponse 写入response * @param returnName 返回的文件名 */ public static void download(String content,HttpServletResponse response, String fileName) throws IOException{ response.setContentType("application/msword"); response.setHeader("Content-Disposition", "attachment; filename="+new String(fileName.getBytes("UTF-8"), "ISO8859-1" )+".doc" ); response.setCharacterEncoding("gb2312"); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byteArrayOutputStream.write(content.getBytes("gb2312")); response.setContentLength(byteArrayOutputStream.size()); OutputStream outputstream = response.getOutputStream(); //取得输出流 byteArrayOutputStream.writeTo(outputstream); //写到输出流 byteArrayOutputStream.close(); //关闭 outputstream.flush(); //刷数据 }
转载于:https://my.oschina.net/kaiqi/blog/3045683