通过url下载文件和本地路径下载文件是不一样的!

该博客展示了两种下载文件的方法:一种是从URL直接下载,使用`FileUtils.copyURLToFile`复制文件;另一种是下载本地文件,通过`FileInputStream`和`OutputStream`读写文件流。代码中设置了响应头以实现附件下载,并处理了异常和资源关闭。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过url下载文件:

    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public void getRequest(HttpServletResponse response) {
        String requestPath = "http://xxx.com/demo/sys/test.pdf"
        String filename = "test.pdf";
        InputStream inputStream = null;
        OutputStream out = null;
        File file = null;
        try {
            file = new File(filename);
            URL url =new URL(requestPath);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
//			conn.setConnectTimeout(1000);//超时提示1秒=1000毫秒
            FileUtils.copyURLToFile(url, file);
//			inputStream =  conn.getInputStream();//获取输出流
            response.setHeader("Content-Disposition", "attachment" + ";filename=\"" + URLEncoder.encode(filename, "UTF-8") + "\"");
            byte[] bytes = new byte[(int)file.length()];
            inputStream = new FileInputStream(file);
            out = response.getOutputStream();
            out.write(bytes, 0, inputStream.read(bytes));
        } catch (Exception e) {
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.flush();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (file != null && file.exists()) {
                file.delete();
            }
        }
    }

下载本地文件:

@RequestMapping(value = "download",method = RequestMethod.GET)
    public ResponseEntity<byte[]> download(HttpServletResponse response, HttpServletRequest request) throws Exception{

        //预下载文件路径******
        String preDownload_path = "C:/Users/Administrator/Desktop/Learn/pdf/temp8.pdf";
        int index = preDownload_path.lastIndexOf("/");
        //下载后的文件名
        String downloadName = preDownload_path.substring(index+1);
        System.out.println(index);
        System.out.println(downloadName);

        response.setCharacterEncoding("utf-8");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;fileName="+java.net.URLEncoder.encode(downloadName,"UTF-8"));

        FileInputStream is = null;
        OutputStream os = null;
        try {
            URL url = new URL(preDownload_path);
            is = new FileInputStream(String.valueOf(url.openStream()));
//            is = new FileInputStream(new File(preDownload_path));
            os = response.getOutputStream();
            byte[] b = new byte[2048];
            int length;
            while ((length = is.read(b)) > 0) {
                os.write(b, 0, length);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            os.close();
            is.close();
        }
        return null;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值