JAVA中URL网络传输的文件的下载工具类

这篇博客介绍了一个JAVA工具类,用于通过URL进行网络文件的下载操作,以`http://192.168.25.133/group1/M00/00/00/wKgZhV2QVYmATWYsAACZeklp3R4679.jpg`为例,展示了如何将文件保存为名为`测试.pnj`的本地文件。

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

public class StreamUtils {

    /**
     * @Description: 将字节的输入流转换成字节数组进行网络传输
     * @Param: InputStream
     * @return:
     * @Author: LingFeng
     * @Date: 2019/11/21
     */
    public static byte[] InputStreamToByte(InputStream inputStream) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
        int len = 0;
        try {
            while ((len = inputStream.read(b)) != -1) {
                byteArrayOutputStream.write(b, 0, len);
            }
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return byteArrayOutputStream.toByteArray();

    }

    /**
     * @Description: 用于对网络传输的文件的下载
     * @Param: URL:网络传输;HttpServletResponse:响应;fileName:文件的名字
     * @return:
     * @Author: LingFeng
     * @Date: 2019/11/21
     *  URL用于网络,所以带有明显的protocol,而且对于中文及符号支持的很不好。
     * File就是我们平常系统中的文件路径了,对于中文及符号都支持,但是已经没有protocol了。
     * 所以,虽然两者都可以表示文件路径,但是却不能混用了。
     *   BufferedInputStream继承于FilterInputStream,提供缓冲输入流功能。缓冲输入流相对于普通输入流的优势是,
     * 它提供了一个缓冲数组,每次调用read方法的时候,它首先尝试从缓冲区里读取数据,若读取失败(缓冲区无可读数据),
     * 则选择从物理数据源(譬如文件)读取新数据(这里会尝试尽可能读取多的字节)放入到缓冲区中,
     * 最后再将缓冲区中的内容部分或全部返回给用户.
     * 由于从缓冲区里读取数据远比直接从物理数据源(譬如文件)读取速度快。
     */

    public static void uploadFileByUrl(URL url, HttpServletResponse response, String fileName) {
        InputStream inputStream = null;//文件的字节输入流
        OutputStream outputStream = null;//文件的字节输出流
        BufferedInputStream bufferedInputStream = null;//输入缓冲流
        BufferedOutputStream bufferedOutputStream = null;//输出缓冲流
        try {
            inputStream = url.openStream();
            bufferedInputStream = new BufferedInputStream(inputStream);
            response.reset();
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
            outputStream = response.getOutputStream();
            bufferedOutputStream = new BufferedOutputStream(outputStream);
            byte[] b = new byte[1024];
            int len = 0;
            while ((len = bufferedInputStream.read(b)) != -1) {
                bufferedOutputStream.write(b, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedInputStream != null) {
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (outputStream != null) {
                try {
                    bufferedOutputStream.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (outputStream != null) {
                try {
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }




}

URL urlConet = new URL("http://192.168.25.133/group1/M00/00/00/wKgZhV2QVYmATWYsAACZeklp3R4679.jpg");

 String fileName=new String("测试.pnj");

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值