com.xiushang.common.upload.utils.ChunkedUploadUtils Maven / Gradle / Ivy
package com.xiushang.common.upload.utils;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* 分块上传工具类
*/
public class ChunkedUploadUtils {
/**
* 内部类记录分块上传文件信息
*/
private static class Value {
String name;
boolean[] status;
Value(MultipartFile file,int n) {
this.name = generateFileName(file);
this.status = new boolean[n];
}
}
private static Map chunkMap = new HashMap<>();
/**
* 判断文件所有分块是否已上传
* @param key
* @return
*/
public static boolean isUploaded(String key) {
if (isExist(key)) {
for (boolean b : chunkMap.get(key).status) {
if (!b) {
return false;
}
}
return true;
}
return false;
}
/**
* 判断文件是否有分块已上传
* @param key
* @return
*/
private static boolean isExist(String key) {
return chunkMap.containsKey(key);
}
/**
* 为文件添加上传分块记录
* @param key
* @param chunk
*/
public static void addChunk(String key, int chunk) {
chunkMap.get(key).status[chunk] = true;
}
/**
* 从map中删除键为key的键值对
* @param key
*/
public static void removeKey(String key) {
if (isExist(key)) {
chunkMap.remove(key);
}
}
/**
* 获取随机生成的文件名
* @param key
* @param chunks
* @return
*/
public static String getFileName(MultipartFile file,String key, int chunks) {
if (!isExist(key)) {
synchronized (ChunkedUploadUtils.class) {
if (!isExist(key)) {
chunkMap.put(key, new Value(file,chunks));
}
}
}
return chunkMap.get(key).name;
}
/**
* 分块写入文件
* @param target
* @param targetSize
* @param src
* @param srcSize
* @param chunks
* @param chunk
* @throws IOException
*/
public static void writeWithBlock(String target, Long targetSize, InputStream src, Long srcSize, Integer chunks, Integer chunk) throws IOException {
RandomAccessFile randomAccessFile = new RandomAccessFile(target,"rw");
randomAccessFile.setLength(targetSize);
if (chunk == chunks - 1) {
randomAccessFile.seek(targetSize - srcSize);
} else {
randomAccessFile.seek(chunk * srcSize);
}
byte[] buf = new byte[1024];
int len;
while (-1 != (len = src.read(buf))) {
randomAccessFile.write(buf,0,len);
}
randomAccessFile.close();
}
/**
* 生成随机文件名
* @return
*/
public static String generateFileName(MultipartFile item) {
StringBuffer sb = new StringBuffer();
String str = String.valueOf(Math.round(Math.random() * 1000000));
sb.append("mg").append(new Date().getTime()).append(str);
sb.append(item.getOriginalFilename().substring(item.getOriginalFilename().lastIndexOf(".")));
return sb.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy