• 下载文件


    1.从网络中下载文件

        @Override
        @RequestMapping("/open/v1/file/downloadFile")
        public void downloadFile(String url, HttpServletResponse response) {
            if (url != null) {
                try {
                    //对文件中文名进行转义处理
                    String fileName = new File(url).getName();
                    url = url.replace(fileName, URLEncoder.encode(fileName, "utf-8"));
    
                    URL urls = new URL(url);
                    URLConnection urlCon = urls.openConnection();
                    InputStream in = urlCon.getInputStream();
                    //设置响应类型
                    response.setContentType("application/octet-stream");//应用程序强制下载
                    response.setContentType("multipart/form-data");
                    //设置响应头,对文件进行url编码
                    String name = URLEncoder.encode(fileName, "UTF-8");
                    response.setHeader("Content-Disposition", "attachment;filename=" + name);
                    response.setContentLength(urlCon.getContentLength());
                    OutputStream out = response.getOutputStream();
                    byte[] b = new byte[1024];
                    int len = 0;
                    while ((len = in.read(b)) != -1) {
                        out.write(b, 0, len);
                    }
                    out.flush();
                    out.close();
                    in.close();
                } catch (IOException e) {
                    logger.error(e.getMessage());
                    throw new SunawException("文件下载异常");
                }
            }
        }

    2.从本地中下载文件

        @Override
        @RequestMapping("/open/v1/file/downloadFile")
        public void downloadFile(String filePath, HttpServletResponse response) {
    
            try {
                //对文件中文名进行转义处理
                File file = new File(filePath);
                String fileName = file.getName();
                
                InputStream in = new FileInputStream(file);
                //设置响应类型
                response.setContentType("application/octet-stream");//应用程序强制下载
                response.setContentType("multipart/form-data");
                //设置响应头,对文件进行url编码
                String name = URLEncoder.encode(fileName, "UTF-8");
                response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
                response.setContentLength(in.available()); //该代码可能有误,没测试过
                OutputStream out = response.getOutputStream();
                byte[] b = new byte[1024];
                int len = 0;
                while ((len = in.read(b)) != -1) {
                    out.write(b, 0, len);
                }
                out.flush();
                out.close();
                in.close();
            } catch (IOException e) {
                logger.error(e.getMessage());
                throw new SunawException("文件下载异常");
            }
        }
  • 相关阅读:
    Redis在windows下安装过程
    Java中如何使用Redis做缓存
    Redis学习记录之Java中的初步使用
    Java多线程实现简单的售票程序
    list,set,map,数组间的相互转换
    高性能Java Web 页面静态化技术(原创)
    Nginx windows下搭建过程
    高并发解决方案--负载均衡
    高并发解决方案--页面静态化
    phpcms 自定义方法
  • 原文地址:https://www.cnblogs.com/tanyucong/p/11770308.html
Copyright © 2020-2023  润新知