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);
}