Jsoup文件下载

package com.swb.utils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Jsoup;

public class JsoupGetFile {

    public static void main(String[] args) throws IOException {
        // 请求地址
        String imageUrl = "http://images.mofcom.gov.cn/trb/202203/20220301163904512.pdf";
        Connection connect = Jsoup.connect(imageUrl);
        Connection.Response response = connect.maxBodySize(5000000).method(Method.GET).ignoreContentType(true).execute();
        System.out.println("文件类型为:" + response.contentType());
        byte[] bufferedInputStream = response.bodyAsBytes();
        // 保存文件到指定目录下
        saveFile(bufferedInputStream, "C:\\Users\\10496\\Desktop\\文档\\photo\\1.pdf");
        //byteToFile(bufferedInputStream,"C:\\Users\\10496\\Desktop\\文档\\photo\\","1.pdf");
    }
    /**
     * saveFile 保存文件操作
     * @param in 字节数组
     * @param savePath 存储路径
     * @throws IOException 异常处理
     */
    static void saveFile(byte[] in, String savePath) throws IOException {
        //byte[] buffer = new byte[1024];
        //int len = 0;
        // 创建缓冲流
        FileOutputStream fileOutStream = new FileOutputStream(new File(savePath));
        BufferedOutputStream bufferedOut = new BufferedOutputStream(fileOutStream);
        // 图片写入
        bufferedOut.write(in, 0, in.length);
        // 缓冲流释放与关闭
        bufferedOut.flush();
        bufferedOut.close();
    }

    /**
     * 字节转换文件
     * @param bfile  字节数组
     * @param filePath   文件夹路径
     * @param fileName    文件名
     */
    public static void byteToFile(byte[] bfile, String filePath,String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            File dir = new File(filePath);
            if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在
                dir.mkdirs();
            }
            file = new File(filePath+File.separator+fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bfile);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
}

注意以下两点:
1,看到有的博文有一个response.bodyStream()方法,我尝试过但貌似行不通,所以就只能直接用数组转换成文件了。
2,Jsoup默认下载文件大小是1M所以很多文件下载下来不完整,记得设置maxBodySize(5000000)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值