Springboot服务端写法:
boolean reportCreated = false; if(breportCreated && reportPath != null && !reportPath.equals("")){ reportFile = new File(reportPath); if(reportFile.exists()) { reportCreated = true; } }
FileInputStream is = new FileInputStream(reportFile); String fileName = vo.getName(); // 设置中文文件名 fileName = new String(fileName.getBytes("utf-8"), "iso8859-1"); String encode = URLEncoder.encode(fileName, "iso8859-1"); ServletOutputStream outputStream = response.getOutputStream(); response.setContentType("application/msword"); response.addHeader("Content-Disposition", "attachment; filename=" + encode + ".docx"); response.setCharacterEncoding("UTF-8"); //用以下代码没有问题(绿色) XWPFDocument document = new XWPFDocument(OPCPackage.open(is)); document.write(outputStream); //用以下代码报错 byte[] buf = new byte[1024]; while(is.read(buf)>0){ outputStream.write(buf,0,buf.length); } document.close(); IOUtils.closeQuietly(outputStream); IOUtils.closeQuietly(is);