java 获取下载流

后台:https://www.cnblogs.com/lucas1024/p/9533220.html
前台:注意不能用ajax获取

<script>
   function downloadImage(){
       var urlString = "http://pic32.nipic.com/20130829/12906030_124355855000_2.png";
       //跳转到后端控制器
       location.href="${ctx}/guest/download.do?urlString="+urlString;
   }

自己写的
前台

image.png

后台

    @GetMapping("/DownloadImg")
    @ResponseBody
    //            File file = new File("C:\\Users\\User\\Desktopimg\\img.png");
    public void tempDownLoad(HttpServletRequest request,HttpServletResponse response) throws FileNotFoundException {

            // 下载本地文件
            String fileName = "Operator.doc".toString(); // 文件的默认保存名
            // 读到流中
            InputStream inStream = new FileInputStream("C:\\Users\\User\\Desktop\\img.png");// 文件的存放路径
            // 设置输出的格式
            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();
            }
        }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。