有关Springboot实现大文件上传、下载

import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

@Component
public class FileUtil {

    public static final int BUFFER_SIZE = 1024 * 1024 * 10; // 10MB

    /**
     * 上传文件
     *
     * @param file     文件
     * @param filePath 保存路径
     * @throws IOException 文件操作异常
     */
    public void upload(MultipartFile file, String filePath) throws IOException {
        File destFile = new File(filePath);
        if (!destFile.getParentFile().exists()) {
            destFile.getParentFile().mkdirs();
        }
        RandomAccessFile raf = new RandomAccessFile(destFile, "rw");
        byte[] buffer = new byte[BUFFER_SIZE];
        InputStream inputStream = file.getInputStream();
        int len;
        while ((len = inputStream.read(buffer)) != -1) {
            raf.write(buffer, 0, len);
        }
        inputStream.close();
        raf.close();
    }

    /**
     * 断点上传文件
     *
     * @param file      文件
     * @param filePath  保存路径
     * @param startPos  起始位置
     * @throws IOException 文件操作异常
     */
    public void upload(MultipartFile file, String filePath, long startPos) throws IOException {
        File destFile = new File(filePath);
        if (!destFile.getParentFile().exists()) {
            destFile.getParentFile().mkdirs();
        }
        RandomAccessFile raf = new RandomAccessFile(destFile, "rw");
        byte[] buffer = new byte[BUFFER_SIZE];
        InputStream inputStream = file.getInputStream();
        int len;
        while ((len = inputStream.read(buffer)) != -1) {
            raf.seek(startPos);
            raf.write(buffer, 0, len);
            startPos += len;
        }
        inputStream.close();
        raf.close();
    }

    /**
     * 下载文件
     *
     * @param fileUrl   文件下载地址
     * @param filePath  保存路径
     * @throws IOException 文件操作异常
     */
    public void download(String fileUrl, String filePath) throws IOException {
        URL url = new URL(fileUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(5000);
        conn.setReadTimeout(5000);
        conn.setRequestProperty("Range", "bytes=0-");
        InputStream inputStream = conn.getInputStream();
        File destFile = new File(filePath);
        if (!destFile.getParentFile().exists()) {
            destFile.getParentFile().mkdirs();
        }
        RandomAccessFile raf = new RandomAccessFile(destFile, "rw");
        byte[] buffer = new byte[BUFFER_SIZE];
        int len;
        while ((len = inputStream.read(buffer)) != -1) {
            raf.write(buffer, 0, len);
        }
        inputStream.close();
        raf.close();
    }

    /**
     * 断点下载文件
     *
     * @param fileUrl   文件下载地址
     * @param filePath  保存路径
     * @param startPos  起始位置
     * @throws IOException 文件操作异常
     */
    public void download(String fileUrl, String filePath, long startPos) throws IOException {
        URL url = new URL(fileUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(5000);
        conn.setReadTimeout(5000);
        conn.setRequestProperty("Range", "bytes=" + startPos + "-");
        InputStream inputStream = conn.getInputStream();
        File destFile = new File(filePath);
        if (!destFile.getParentFile().exists()) {
            destFile.getParentFile().mkdirs();
        }
        RandomAccessFile raf = new RandomAccessFile(destFile, "rw");
        byte[] buffer = new byte[BUFFER_SIZE];
        int len;
        while ((len = inputStream.read(buffer)) != -1) {
            raf.seek(startPos);
            raf.write(buffer, 0, len);
            startPos += len;
        }
        inputStream.close();
        raf.close();
    }
}

使用方法

上传文件

@Autowired
private FileUtil fileUtil;

@PostMapping("/upload")
public void upload(@RequestParam("file") MultipartFile file) throws IOException {
    fileUtil.upload(file, "文件保存路径");
}

断点上传文件

@Autowired
private FileUtil fileUtil;

@PostMapping("/upload")
public void upload(@RequestParam("file") MultipartFile file, @RequestParam("startPos") long startPos) throws IOException {
    fileUtil.upload(file, "文件保存路径", startPos);
}

下载文件

@Autowired
private FileUtil fileUtil;

@GetMapping("/download")
public void download(@RequestParam("fileUrl") String fileUrl) throws IOException {
    fileUtil.download(fileUrl, "文件保存路径");
}

断点下载文件

@Autowired
private FileUtil fileUtil;

@GetMapping("/download")
public void download(@RequestParam("fileUrl") String fileUrl, @RequestParam("startPos") long startPos) throws IOException {
    fileUtil.download(fileUrl, "文件保存路径", startPos);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

还算善良_

如果对你的工作有所帮助,拜托啦

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

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

打赏作者

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

抵扣说明:

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

余额充值