问题描述
附件下载,为什么点击下载的时候是把我的方法.do下载下来了,代码如下@RequestMappingpublic void viewFileUpload(Long fileId, HttpServletResponse response) throws Exception{AssetBase file = assetBaseService.findByPrimaryKey(fileId);/*ServletOutputStream out = response.getOutputStream();out.write(file.getFileData());out.flush();out.close();*/byte[] data = file.getFileData();response.reset();response.setHeader("Content-Disposition", "attachment;"); response.setContentType("application/octet-stream;charset=UTF-8"); OutputStream outputStream = new BufferedOutputStream(response.getOutputStream()); outputStream.write(data); outputStream.flush(); outputStream.close(); }
解决方案
你试试这种方式 public void downloadLocal(HttpServletResponse response) throws FileNotFoundException { // 下载本地文件 String fileName = "Operator.doc".toString(); // 文件的默认保存名 // 读到流中 InputStream inStream = new FileInputStream("c:/Operator.doc");// 文件的存放路径 // 设置输出的格式 response.reset(); response.setContentType("bin"); response.addHeader("Content-Disposition", "attachment; filename="" + fileName + """); // 循环取出流中的数据 byte[] b = new byte[100]; int len; try { while ((len = inStream.read(b)) > 0) response.getOutputStream().write(b, 0, len); inStream.close(); } catch (IOException e) { e.printStackTrace(); } }个人签名:健康宝宝
解决方案二:
response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));