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

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

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