ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

根据本地文件路径下载文件与保存指定url文件工具类

2021-03-04 13:57:35  阅读:162  来源: 互联网

标签:文件 outputStream url 路径 read import new


两个工具方法

1、下载文件到浏览器

2、保存指定url的文件


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;


/**
 * 文件下载
 *
 */
@Slf4j
public class FileDownloadUtil {

    /**
     * http 传输文件
     *
     * @param response servlet响应
     * @param filePath 文件全路径名
     * @param fileName 附件文件名
     */
    public static void download(HttpServletResponse response,
                                String filePath,
                                String fileName,
                                String contentType
    ) throws IOException {

        if (StringUtils.isBlank(contentType)) {
            response.setContentType("application/octet-stream");
        } else {
            response.setContentType(contentType);
        }

        try {
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "utf-8"));
        } catch (Exception e) {
            // pass
        }

        byte[] buff = new byte[1024];

        //创建缓冲输入流
        BufferedInputStream bis = null;
        OutputStream outputStream = null;

        try {
            outputStream = response.getOutputStream();

            //这个路径为待下载文件的路径
            bis = new BufferedInputStream(new FileInputStream(new File(filePath)));
            int read = bis.read(buff);

            //通过while循环写入到指定了的文件夹中
            while (read != -1) {
                outputStream.write(buff, 0, read);
                outputStream.flush();
                read = bis.read(buff);
            }
        } finally {
            if (null != bis) {
                try {
                    bis.close();
                } catch (IOException e) {
                    // 忽略
                }
            }
            if (null != outputStream) {
                try {
                    outputStream.flush();
                    outputStream.close();
                } catch (IOException e) {
                    // 忽略
                }
            }
        }
    }


    /**
     * 用http请求将url文件保存到本地
     *
     * @param url      请求文件的url
     * @param filePath 需要保存的路径
     * @return 是否保存文件成功, 成功返回true, 失败返回false
     */
    public static boolean saveFileByUrl(URL url, String filePath) {
        try {
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(10000);
            connection.setReadTimeout(10000);
            DataInputStream inputStream = new DataInputStream(connection.getInputStream());
            DataOutputStream outputStream = new DataOutputStream(new FileOutputStream(filePath));
            byte[] buffer = new byte[1024];
            int count;
            while ((count = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, count);
            }
            outputStream.close();
            inputStream.close();
            log.info("this is connection success");
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}

标签:文件,outputStream,url,路径,read,import,new
来源: https://blog.csdn.net/weixin_45095479/article/details/114366636

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有