修复文件上传并发问题
This commit is contained in:
parent
1b3ac1c8df
commit
27e85b00ec
|
@ -1,6 +1,7 @@
|
||||||
package com.len.util;
|
package com.len.util;
|
||||||
|
|
||||||
import com.len.exception.MyException;
|
import com.len.exception.MyException;
|
||||||
|
import lombok.Data;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
|
@ -25,11 +26,6 @@ import java.util.UUID;
|
||||||
@Component
|
@Component
|
||||||
public class UploadUtil {
|
public class UploadUtil {
|
||||||
|
|
||||||
/**
|
|
||||||
* 上传请求资源
|
|
||||||
*/
|
|
||||||
private MultipartFile multipartFile = null;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 按照当日创建文件夹
|
* 按照当日创建文件夹
|
||||||
*/
|
*/
|
||||||
|
@ -46,27 +42,22 @@ public class UploadUtil {
|
||||||
|
|
||||||
public static final String IMAGE_SUFFIX = "bmp,jpg,png,gif,jpeg";
|
public static final String IMAGE_SUFFIX = "bmp,jpg,png,gif,jpeg";
|
||||||
|
|
||||||
private File currentFile;
|
|
||||||
|
|
||||||
public UploadUtil(MultipartFile multipartFile) {
|
|
||||||
this.multipartFile = multipartFile;
|
|
||||||
}
|
|
||||||
|
|
||||||
public UploadUtil() {
|
public UploadUtil() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String upload() {
|
public String upload(MultipartFile multipartFile) {
|
||||||
if (isNull()) {
|
if (isNull(multipartFile)) {
|
||||||
throw new MyException("上传数据/地址获取异常");
|
throw new MyException("上传数据/地址获取异常");
|
||||||
}
|
}
|
||||||
|
|
||||||
String fileName = fileNameStyle();
|
LoadType loadType = fileNameStyle(multipartFile);
|
||||||
try {
|
try {
|
||||||
FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), currentFile);
|
FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), loadType.getCurrentFile());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return fileName;
|
return loadType.getFileName();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -74,7 +65,7 @@ public class UploadUtil {
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public String fileNameStyle() {
|
public LoadType fileNameStyle(MultipartFile multipartFile) {
|
||||||
String curr = multipartFile.getOriginalFilename();
|
String curr = multipartFile.getOriginalFilename();
|
||||||
int suffixLen = curr.lastIndexOf(".");
|
int suffixLen = curr.lastIndexOf(".");
|
||||||
if (suffixLen == -1) {
|
if (suffixLen == -1) {
|
||||||
|
@ -85,21 +76,26 @@ public class UploadUtil {
|
||||||
suffix.replace(".", ""));
|
suffix.replace(".", ""));
|
||||||
|
|
||||||
curr = UUID.randomUUID() + suffix;
|
curr = UUID.randomUUID() + suffix;
|
||||||
String filename=curr;
|
LoadType loadType = new LoadType();
|
||||||
|
loadType.setFileName(curr);
|
||||||
//image 情况
|
//image 情况
|
||||||
curr = StringUtils.isEmpty(imagePath) || index == -1 ?
|
curr = StringUtils.isEmpty(imagePath) || index == -1 ?
|
||||||
uploadPath + File.separator + curr : imagePath + File.separator + curr;
|
uploadPath + File.separator + curr : imagePath + File.separator + curr;
|
||||||
currentFile = new File(curr);
|
loadType.setCurrentFile(new File(curr));
|
||||||
|
return loadType;
|
||||||
return filename;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isNull() {
|
private boolean isNull(MultipartFile multipartFile) {
|
||||||
if (null != multipartFile) {
|
if (null != multipartFile) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
class LoadType {
|
||||||
|
private String fileName;
|
||||||
|
private File currentFile;
|
||||||
}
|
}
|
||||||
|
|
|
@ -240,8 +240,7 @@ public class UserController extends BaseController {
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public JsonUtil imgUpload(HttpServletRequest req, @RequestParam("file") MultipartFile file,
|
public JsonUtil imgUpload(HttpServletRequest req, @RequestParam("file") MultipartFile file,
|
||||||
ModelMap model) {
|
ModelMap model) {
|
||||||
uploadUtil.setMultipartFile(file);
|
String fileName=uploadUtil.upload(file);
|
||||||
String fileName=uploadUtil.upload();
|
|
||||||
JsonUtil j = new JsonUtil();
|
JsonUtil j = new JsonUtil();
|
||||||
j.setMsg(fileName);
|
j.setMsg(fileName);
|
||||||
return j;
|
return j;
|
||||||
|
|
Loading…
Reference in New Issue