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

View File

@ -840,7 +840,7 @@ public class FileUtil extends PathUtil {
int exceptionsCount = 0; int exceptionsCount = 0;
while (true) { while (true) {
try { try {
File file = File.createTempFile(prefix, suffix, dir).getCanonicalFile(); File file = File.createTempFile(prefix, suffix, mkdir(dir)).getCanonicalFile();
if (isReCreat) { if (isReCreat) {
//noinspection ResultOfMethodCallIgnored //noinspection ResultOfMethodCallIgnored
file.delete(); file.delete();
@ -978,6 +978,8 @@ public class FileUtil extends PathUtil {
* @see PathUtil#move(Path, Path, boolean) * @see PathUtil#move(Path, Path, boolean)
*/ */
public static void move(File src, File target, boolean isOverride) throws IORuntimeException { 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); move(src.toPath(), target.toPath(), isOverride);
} }

View File

@ -71,10 +71,10 @@ public class MultipartFormData {
if (fileName.length() > 0 && header.contentType.contains("application/x-macbinary")) { if (fileName.length() > 0 && header.contentType.contains("application/x-macbinary")) {
input.skipBytes(128); input.skipBytes(128);
} }
UploadFile newFile = new UploadFile(header, setting); final UploadFile newFile = new UploadFile(header, setting);
newFile.processStream(input); if(newFile.processStream(input)){
putFile(header.formFieldName, newFile); putFile(header.formFieldName, newFile);
}
} else { } else {
// 标准表单项 // 标准表单项
putParameter(header.formFieldName, input.readString(charset)); putParameter(header.formFieldName, input.readString(charset));

View File

@ -9,6 +9,7 @@ import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.NoSuchFileException;
/** /**
* 上传的文件对象 * 上传的文件对象
@ -86,12 +87,19 @@ public class UploadFile {
destination = new File(destination, this.header.getFileName()); destination = new File(destination, this.header.getFileName());
} }
if (data != null) { if (data != null) {
// 内存中
FileUtil.writeBytes(data, destination); FileUtil.writeBytes(data, destination);
data = null; data = null;
} else { } 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; return destination;
} }

View File

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