add uoload check

This commit is contained in:
Looly 2021-01-22 22:41:49 +08:00
parent 93df8333df
commit 103f352cf4
5 changed files with 35 additions and 24 deletions

View File

@ -10,6 +10,7 @@
* 【extra 】 ArrayUtil增加map方法重载
* 【crypto 】 AsymmetricAlgorithm增加RSA_ECB("RSA/ECB/NoPadding")issue#1368@Github
* 【core 】 补充StrUtil.padXXX注释issue#I2E1S7@Gitee
* 【core 】 修改上传文件检查逻辑
### Bug修复
* 【core 】 修复FileUtil.move以及PathUtil.copy等无法自动创建父目录的问题issue#I2CKTI@Gitee

View File

@ -840,7 +840,7 @@ public class FileUtil extends PathUtil {
int exceptionsCount = 0;
while (true) {
try {
File file = File.createTempFile(prefix, suffix, dir).getCanonicalFile();
File file = File.createTempFile(prefix, suffix, mkdir(dir)).getCanonicalFile();
if (isReCreat) {
//noinspection ResultOfMethodCallIgnored
file.delete();
@ -978,6 +978,8 @@ public class FileUtil extends PathUtil {
* @see PathUtil#move(Path, Path, boolean)
*/
public static void move(File src, File target, boolean isOverride) throws IORuntimeException {
Assert.notNull(src, "Src file must be not null!");
Assert.notNull(target, "target file must be not null!");
move(src.toPath(), target.toPath(), isOverride);
}

View File

@ -14,7 +14,7 @@ import java.util.Set;
/**
* HttpRequest解析器<br>
* 来自Jodd
*
*
* @author jodd.org
*/
public class MultipartFormData {
@ -39,7 +39,7 @@ public class MultipartFormData {
/**
* 构造
*
*
* @param uploadSetting 上传设定
*/
public MultipartFormData(UploadSetting uploadSetting) {
@ -49,7 +49,7 @@ public class MultipartFormData {
/**
* 提取上传的文件和表单数据
*
*
* @param inputStream HttpRequest流
* @param charset 编码
* @throws IOException IO异常
@ -71,10 +71,10 @@ public class MultipartFormData {
if (fileName.length() > 0 && header.contentType.contains("application/x-macbinary")) {
input.skipBytes(128);
}
UploadFile newFile = new UploadFile(header, setting);
newFile.processStream(input);
putFile(header.formFieldName, newFile);
final UploadFile newFile = new UploadFile(header, setting);
if(newFile.processStream(input)){
putFile(header.formFieldName, newFile);
}
} else {
// 标准表单项
putParameter(header.formFieldName, input.readString(charset));
@ -96,7 +96,7 @@ public class MultipartFormData {
// ---------------------------------------------------------------- parameters
/**
* 返回单一参数值如果有多个只返回第一个
*
*
* @param paramName 参数名
* @return null未找到否则返回值
*/
@ -117,7 +117,7 @@ public class MultipartFormData {
/**
* 获得数组表单值
*
*
* @param paramName 参数名
* @return 数组表单值
*/
@ -142,7 +142,7 @@ public class MultipartFormData {
/**
* 获取所有属性的集合
*
*
* @return 所有属性的集合
*/
public Map<String, String[]> getParamMap() {
@ -161,7 +161,7 @@ public class MultipartFormData {
// --------------------------------------------------------------------------- Files parameters
/**
* 获取上传的文件
*
*
* @param paramName 文件参数名称
* @return 上传的文件 如果无为null
*/
@ -176,7 +176,7 @@ public class MultipartFormData {
/**
* 获得某个属性名的所有文件<br>
* 当表单中两个文件使用同一个name的时候
*
*
* @param paramName 属性名
* @return 上传的文件列表
*/
@ -202,7 +202,7 @@ public class MultipartFormData {
/**
* 获取上传的文件属性名集合
*
*
* @return 上传的文件属性名集合
*/
public Set<String> getFileParamNames() {
@ -211,7 +211,7 @@ public class MultipartFormData {
/**
* 获取文件映射
*
*
* @return 文件映射
*/
public Map<String, UploadFile[]> getFileMap() {
@ -230,7 +230,7 @@ public class MultipartFormData {
// --------------------------------------------------------------------------- Load
/**
* 是否已被解析
*
*
* @return 如果流已被解析返回true
*/
public boolean isLoaded() {
@ -240,7 +240,7 @@ public class MultipartFormData {
// ---------------------------------------------------------------- Private method start
/**
* 加入上传文件
*
*
* @param name 参数名
* @param uploadFile 文件
*/
@ -250,7 +250,7 @@ public class MultipartFormData {
/**
* 加入普通参数
*
*
* @param name 参数名
* @param value 参数值
*/
@ -260,7 +260,7 @@ public class MultipartFormData {
/**
* 设置使输入流为解析状态如果已解析则抛出异常
*
*
* @throws IOException IO异常
*/
private void setLoaded() throws IOException {

View File

@ -9,6 +9,7 @@ import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.NoSuchFileException;
/**
* 上传的文件对象
@ -86,12 +87,19 @@ public class UploadFile {
destination = new File(destination, this.header.getFileName());
}
if (data != null) {
// 内存中
FileUtil.writeBytes(data, destination);
data = null;
} else {
if (tempFile != null) {
FileUtil.move(tempFile, destination, true);
// 临时文件
if(null == this.tempFile){
throw new NullPointerException("Temp file is null !");
}
if(false == this.tempFile.exists()){
throw new NoSuchFileException("Temp file: [" + this.tempFile.getAbsolutePath() + "] not exist!");
}
FileUtil.move(tempFile, destination, true);
}
return destination;
}

View File

@ -51,10 +51,10 @@ public class UploadTest {
@Test
@Ignore
public void uploadTest() {
public void uploadTest2() {
//客户端
String url = "http://localhost:8888/file";
Path file = Paths.get("D:\\testBigData_upload.xlsx");
String url = "http://192.168.1.200:8888/meta/upload/img";
Path file = Paths.get("D:\\test\\testBigData_upload.xlsx");
Map<String, String> headers = new HashMap<>(16);
headers.put("md5", "aaaaaaaa");