java 文件下载

 

import org.apache.commons.io.IOUtils;
import org.aspectj.util.FileUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class OxFileUtils {

    /***
     * 文件流列表 压缩包 下载
     *
     * @param response
     * @param inputStreamList 文件流集合
     * @param name 对应文件流生成的文件名称集合
     */
    public static void zipFile(HttpServletResponse response, List<InputStream> inputStreamList, List<String> name, String zipName) {

        try {
            //转换中文,避免乱码
            zipName = URLEncoder.encode(zipName, "UTF-8");
            //返回文件流
            response.setContentType("application/octet-stream");
            //设置下载框默认显示得的文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + zipName);

            byte[] buf = new byte[1024];
            ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());
            zipOutputStream.setMethod(ZipOutputStream.DEFLATED);

            int i = 0;
            for (InputStream inputStream : inputStreamList) {

                zipOutputStream.putNextEntry(new ZipEntry(name.get(i)));
                i++;
                int len;
                while ((len = inputStream.read(buf)) > 0) {
                    zipOutputStream.write(buf, 0, len);
                }
                zipOutputStream.closeEntry();
                inputStream.close();
            }
            zipOutputStream.close();
        } catch (Exception e) {
        } finally {
            for (InputStream inputStream : inputStreamList) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                }
            }
        }
    }


    /***
     * 单文件流 下载
     *
     * @param response
     * @param inputStream 文件流集合
     * @param name 对应文件流生成的文件名称集合
     */
    public static void download(HttpServletResponse response, InputStream inputStream, String name) {

        try {
            //转换中文,避免乱码
            name = URLEncoder.encode(name, "UTF-8");
            //返回文件流
            response.setContentType("application/octet-stream");
            //设置下载框默认显示得的文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + name);
            IOUtils.copy(inputStream, response.getOutputStream());
            response.flushBuffer();
        } catch (Exception e) {
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                }
            }
        }
    }


    /***
     * 网址下载
     *
     * @param response
     * @param url 网址
     * @param name 对应文件流生成的文件名称集合
     */
    public static void download(HttpServletResponse response, String url, String name) {

        InputStream inputStream = null;
        try {
            //转换中文,避免乱码
            name = URLEncoder.encode(name, "UTF-8");
            //返回文件流
            response.setContentType("application/octet-stream");
            //设置下载框默认显示得的文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + name);

            inputStream = OxFileUtils.getInputStreamByUrl(url);
            IOUtils.copy(inputStream, response.getOutputStream());
            response.flushBuffer();
        } catch (Exception e) {

        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                }
            }
        }
    }

    /***
     * 网址下载
     *
     * @param urlString 网址
     * @return InputStream 文件流
     */
    public static InputStream getInputStreamByUrl(String urlString) {

        try {
            //定义一个URL对象,就是你想下载的图片的URL地址
            URL url = new URL(urlString);
            //打开连接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //设置请求方式为"GET"
            conn.setRequestMethod("GET");
            //超时响应时间为10秒
            conn.setConnectTimeout(10 * 1000);
            //通过输入流获取图片数据
            return conn.getInputStream();
        } catch (Exception e) {

        }
    }

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值