Java实现网络文件下载、下载到指定目录

本文介绍在Spring框架下如何使用StreamUtils和FileCopyUtils工具类实现PDF文件的下载功能,包括从URL获取输入流、设置HTTP响应头以及使用工具类复制流。

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

前言

最近项目中有使用到pdf文件下载功能,在使用过程中,发现spring给我们提供了两个工具类,已经实现了文件下载的功能:StreamUtils和FileCopyUtils,都是org.springframework.util包下面的类,使用非常方便。

相关代码

service实现类

	public InputStream getDownloadInputStreamByUrl(String downloadUrl) {
		if (ObjectUtils.isEmpty(downloadUrl)) {
            throw new RuntimeException("无效参数:fileUrl 不能为空");
        }
		
		InputStream in = null;
		try {
			URL url = new URL(downloadUrl);

			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			// 设置超时时间为3秒
			conn.setConnectTimeout(3 * 1000);
			// 防止屏蔽程序抓取而返回403错误
			conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
			// 得到输入流
			in = conn.getInputStream();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return in;
	}

Controller类

	@GetMapping("/fileDownload/{filename}")
    public void downloadPdfFile(HttpServletResponse response, @PathVariable(name="filename") String filename) {
        try {
            if (StringUtils.isEmpty(filename)) {
            	throw new RuntimeException("文件名不可为空!");
            }
            //通过文件名查找文件对应的url
            String downloadUrl = fileDownloadService.getUrlByFiname(filename);
            if (StringUtils.isEmpty(filename)) {
            	throw new RuntimeException("文件对应地址不存在");
            }
            // 此处异常方法中已处理
            InputStream in = fileDownloadService.getDownloadInputStreamByUrl(downloadUrl);
   			// 设置编码格式
            response.setCharacterEncoding("UTF-8");
            /* 若要下载指定目录,以下两行需注释,否则会下载两份,一份到指定目录,一份到浏览器下载目录 */
            // 设置下载格式
            response.setHeader("Content-Type", "application/pdf");
            // 注释这行代码,可实现 预览 功能(只限pdf文件)
            response.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
			//下载到指定目录
			/*
			File file = new File("C:\\tmp");
            if (!file.exists()) {
                //如果文件夹不存在,则创建新的的文件夹
                file.mkdirs();
            }

            File file1 = new File(file.getPath() + File.separator + filename);
           	OutputStream outputStream = new FileOutputStream(file1);
           	FileCopyUtils.copy(in, outputStream);
           	*/

            StreamUtils.copy(in, response.getOutputStream());
        } catch (Exception e) {
        	throw new RuntimeException(e);
        } 
    }

此处通过spring提供的工具类进行文件下载。工具类中实现方式:在这里插入图片描述
此处也可通过FileCopyUtils.copy(InputStream input, OutputStream output)实现文件下载,其底层也是通过调用StreamUtils.copy(InputStream in, OutputStream out)方法实现的,不过此方法最后通过finally关闭了InputStream和OutputStream,逻辑更严谨。
在这里插入图片描述
这两个工具类中还有其他的导出方法,可以传递其他参数进去,如图:在这里插入图片描述
在这里插入图片描述
可以看到,这两个工具类中基本涵盖了文件下载所需的所有操作。
以上代码中对异常只是做了简单的 throw new RuntimeException() 操作,读者可根据自己的项目对异常进行处理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值