java下载文件

工具类

import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

@Slf4j
public class FileUtils {

    /*
     * @description  获取指定url的文件(非本地)
     * @param url 文件路径
     * @return file
     */
    public static File getfile(String url) throws MalformedURLException {
        URL imgurl = new URL(url);
        File file = null;
        InputStream inStream = null;
        OutputStream os =null;
        try {
            file = File.createTempFile("url", "newfile");
            inStream = imgurl.openStream();
            os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = inStream.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (null != os) {
                    os.close();
                }
                if (null != inStream) {
                    inStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return file;
    }

下载方法

@RequestMapping(value = "/doDownload", method = {RequestMethod.POST})
    @ApiOperation(value = "下载文件",notes = "下载文件",response = RespResult.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "url", value = "文件地址"),
    })
    public void downloadInstructionsFile( String url, HttpServletResponse response) throws MalformedURLException {
        log.info("文件路径:"+url);
        InputStream inputStream = null;
        OutputStream outputStream = null;
        //通过工具获取服务的文件进行实例化
        File file = FileUtils.getfile(url);
        //设置文件名
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String formatTime = DateUtil.format(DateUtil.date(), "yyyyMMddHHmm-");
        try{
            if(file.exists()){
                inputStream = new FileInputStream(file);
                outputStream = response.getOutputStream();
                response.reset();
                response.setHeader("Access-Control-Allow-Origin", "http://项目域名");
                response.setContentType("application/x-download;charset=GBK");
                response.setContentLength((int)file.length());
                response.setHeader("Content-Disposition", "attachment;filename="+ new String((formatTime+uuid).getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
                IOUtils.copy(inputStream,outputStream);
                response.getOutputStream().flush();
            }else{
                log.info("文件不存在");
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(inputStream != null){
                    inputStream.close();
                }
                if(outputStream != null){
                    outputStream.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Herbig

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值