@RestController public class Test { @GetMapping("test") public void test(HttpServletResponse response) throws IOException { response.setCharacterEncoding("utf-8"); response.setContentType("application/msword"); // 设置浏览器以下载的方式处理该文件名 response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode("fileName.docx", "UTF-8")))); File file = new File("D://testWord/test0.docx"); InputStream fis = new BufferedInputStream(new FileInputStream(file)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); toClient.write(buffer); toClient.flush(); toClient.close(); } }
java 向浏览器输出world
最新推荐文章于 2024-03-28 22:52:05 发布