上传文件异常处理

This commit is contained in:
马超 2021-03-15 19:35:14 +08:00
parent e8e5909abf
commit 8ca8f27c67
8 changed files with 96 additions and 95 deletions

View File

@ -43,28 +43,12 @@
<artifactId>jjwt</artifactId> <artifactId>jjwt</artifactId>
<version>0.9.1</version> <version>0.9.1</version>
</dependency> </dependency>
<!-- <dependency>-->
<!-- <groupId>org.csource</groupId>-->
<!-- <artifactId>fastdfs-client-java</artifactId>-->
<!-- <version>1.27-SNAPSHOT</version>-->
<!-- </dependency>-->
<!-- https://mvnrepository.com/artifact/net.oschina.zcx7878/fastdfs-client-java -->
<!-- <dependency>-->
<!-- <groupId>net.oschina.zcx7878</groupId>-->
<!-- <artifactId>fastdfs-client-java</artifactId>-->
<!-- <version>1.27.0.0</version>-->
<!-- </dependency>-->
<dependency> <dependency>
<groupId>com.github.tobato</groupId> <groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId> <artifactId>fastdfs-client</artifactId>
<version>1.26.2</version> <version>1.26.2</version>
</dependency> </dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies> </dependencies>

View File

@ -0,0 +1,12 @@
package com.qiwenshare.common.exception;
public abstract class UploadException extends RuntimeException{
protected UploadException(String message) {
super(message);
}
protected UploadException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -0,0 +1,16 @@
package com.qiwenshare.common.exception;
public class UploadGeneralException extends UploadException{
public UploadGeneralException(Throwable cause) {
super("上传出现了异常", cause);
}
public UploadGeneralException(String message) {
super(message);
}
public UploadGeneralException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -27,26 +27,7 @@ public abstract class Uploader {
protected StandardMultipartHttpServletRequest request = null; protected StandardMultipartHttpServletRequest request = null;
public abstract List<UploadFile> upload(HttpServletRequest request); public abstract List<UploadFile> upload(HttpServletRequest request);
//
//
// protected static TrackerServer trackerServer = null;
//
// protected static StorageServer storageServer = null;
//
// static {
// try {
// //String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();;
// Properties properties = new Properties();
// properties.setProperty("tracker_server ", "121.89.222.103");
// properties.setProperty("http.tracker_http_port", "8090");
// ClientGlobal.initByProperties(properties);
// TrackerClient trackerClient = new TrackerClient();
// trackerServer = trackerClient.getConnection();
// storageServer = trackerClient.getStoreStorage(trackerServer);
// } catch (Exception e) {
// log.error("FastDFS Client Init Fail!",e);
// }
// }
/** /**
* 根据字符串创建本地目录 并按照日期建立子目录返回 * 根据字符串创建本地目录 并按照日期建立子目录返回
* *

View File

@ -6,6 +6,7 @@ import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.*; import com.aliyun.oss.model.*;
import com.qiwenshare.common.domain.AliyunOSS; import com.qiwenshare.common.domain.AliyunOSS;
import com.qiwenshare.common.domain.UploadFile; import com.qiwenshare.common.domain.UploadFile;
import com.qiwenshare.common.exception.UploadGeneralException;
import com.qiwenshare.common.upload.Uploader; import com.qiwenshare.common.upload.Uploader;
import com.qiwenshare.common.util.FileUtil; import com.qiwenshare.common.util.FileUtil;
import com.qiwenshare.common.util.PathUtil; import com.qiwenshare.common.util.PathUtil;
@ -63,17 +64,17 @@ public class AliyunOSSUploader extends Uploader {
bucketName = aliyunOSS.getBucketName(); bucketName = aliyunOSS.getBucketName();
boolean isMultipart = ServletFileUpload.isMultipartContent(this.request); boolean isMultipart = ServletFileUpload.isMultipartContent(this.request);
if (!isMultipart) { if (!isMultipart) {
UploadFile uploadFile = new UploadFile(); throw new UploadGeneralException("未包含文件上传域");
uploadFile.setSuccess(0); // UploadFile uploadFile = new UploadFile();
uploadFile.setMessage("未包含文件上传域"); // uploadFile.setSuccess(0);
saveUploadFileList.add(uploadFile); // uploadFile.setMessage("未包含文件上传域");
return saveUploadFileList; // saveUploadFileList.add(uploadFile);
// return saveUploadFileList;
} }
DiskFileItemFactory dff = new DiskFileItemFactory();//1创建工厂 DiskFileItemFactory dff = new DiskFileItemFactory();//1创建工厂
String savePath = getSaveFilePath(); String savePath = getSaveFilePath();
dff.setRepository(new File(savePath)); dff.setRepository(new File(savePath));
try {
ServletFileUpload sfu = new ServletFileUpload(dff);//2创建文件上传解析器 ServletFileUpload sfu = new ServletFileUpload(dff);//2创建文件上传解析器
sfu.setSizeMax(this.maxSize * 1024L); sfu.setSizeMax(this.maxSize * 1024L);
sfu.setHeaderEncoding("utf-8");//3解决文件名的中文乱码 sfu.setHeaderEncoding("utf-8");//3解决文件名的中文乱码
@ -82,19 +83,13 @@ public class AliyunOSSUploader extends Uploader {
saveUploadFileList = doUpload(savePath, iter); saveUploadFileList = doUpload(savePath, iter);
} }
} catch (IOException e) {
UploadFile uploadFile = new UploadFile();
uploadFile.setSuccess(1);
uploadFile.setMessage("未知错误");
saveUploadFileList.add(uploadFile);
e.printStackTrace();
}
logger.info("结束上传"); logger.info("结束上传");
return saveUploadFileList; return saveUploadFileList;
} }
private List<UploadFile> doUpload(String savePath, Iterator<String> iter) throws IOException { private List<UploadFile> doUpload(String savePath, Iterator<String> iter) {
OSS ossClient = getClient(); OSS ossClient = getClient();
List<UploadFile> saveUploadFileList = new ArrayList<>(); List<UploadFile> saveUploadFileList = new ArrayList<>();
@ -174,6 +169,7 @@ public class AliyunOSSUploader extends Uploader {
} catch (Exception e) { } catch (Exception e) {
logger.error("上传出错:" + e); logger.error("上传出错:" + e);
throw new UploadGeneralException(e);
} }
uploadFile.setIsOSS(1); uploadFile.setIsOSS(1);

View File

@ -2,6 +2,7 @@ package com.qiwenshare.common.upload.product;
import com.qiwenshare.common.cbb.NotSameFileExpection; import com.qiwenshare.common.cbb.NotSameFileExpection;
import com.qiwenshare.common.domain.UploadFile; import com.qiwenshare.common.domain.UploadFile;
import com.qiwenshare.common.exception.UploadGeneralException;
import com.qiwenshare.common.operation.ImageOperation; import com.qiwenshare.common.operation.ImageOperation;
import com.qiwenshare.common.upload.Uploader; import com.qiwenshare.common.upload.Uploader;
import com.qiwenshare.common.util.FileUtil; import com.qiwenshare.common.util.FileUtil;
@ -42,11 +43,12 @@ public class ChunkUploader extends Uploader {
this.request = (StandardMultipartHttpServletRequest) httpServletRequest; this.request = (StandardMultipartHttpServletRequest) httpServletRequest;
boolean isMultipart = ServletFileUpload.isMultipartContent(this.request); boolean isMultipart = ServletFileUpload.isMultipartContent(this.request);
if (!isMultipart) { if (!isMultipart) {
UploadFile uploadFile = new UploadFile(); throw new UploadGeneralException("未包含文件上传域");
uploadFile.setSuccess(0); // UploadFile uploadFile = new UploadFile();
uploadFile.setMessage("未包含文件上传域"); // uploadFile.setSuccess(0);
saveUploadFileList.add(uploadFile); // uploadFile.setMessage("未包含文件上传域");
return saveUploadFileList; // saveUploadFileList.add(uploadFile);
// return saveUploadFileList;
} }
DiskFileItemFactory dff = new DiskFileItemFactory();//1创建工厂 DiskFileItemFactory dff = new DiskFileItemFactory();//1创建工厂
String savePath = getSaveFilePath(); String savePath = getSaveFilePath();
@ -61,11 +63,12 @@ public class ChunkUploader extends Uploader {
saveUploadFileList = doUpload(savePath, iter); saveUploadFileList = doUpload(savePath, iter);
} }
} catch (IOException e) { } catch (IOException e) {
UploadFile uploadFile = new UploadFile(); // UploadFile uploadFile = new UploadFile();
uploadFile.setSuccess(1); // uploadFile.setSuccess(1);
uploadFile.setMessage("未知错误"); // uploadFile.setMessage("未知错误");
saveUploadFileList.add(uploadFile); // saveUploadFileList.add(uploadFile);
e.printStackTrace(); // e.printStackTrace();
throw new UploadGeneralException("未包含文件上传域");
} catch (NotSameFileExpection notSameFileExpection) { } catch (NotSameFileExpection notSameFileExpection) {
notSameFileExpection.printStackTrace(); notSameFileExpection.printStackTrace();
} }

View File

@ -3,6 +3,7 @@ package com.qiwenshare.common.upload.product;
import com.github.tobato.fastdfs.domain.StorePath; import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.AppendFileStorageClient; import com.github.tobato.fastdfs.service.AppendFileStorageClient;
import com.qiwenshare.common.domain.UploadFile; import com.qiwenshare.common.domain.UploadFile;
import com.qiwenshare.common.exception.UploadGeneralException;
import com.qiwenshare.common.upload.Uploader; import com.qiwenshare.common.upload.Uploader;
import com.qiwenshare.common.util.FileUtil; import com.qiwenshare.common.util.FileUtil;
import com.qiwenshare.common.util.PathUtil; import com.qiwenshare.common.util.PathUtil;
@ -50,11 +51,12 @@ public class FastDFSUploader extends Uploader {
boolean isMultipart = ServletFileUpload.isMultipartContent(this.request); boolean isMultipart = ServletFileUpload.isMultipartContent(this.request);
if (!isMultipart) { if (!isMultipart) {
UploadFile uploadFile = new UploadFile(); throw new UploadGeneralException("未包含文件上传域");
uploadFile.setSuccess(0); // UploadFile uploadFile = new UploadFile();
uploadFile.setMessage("未包含文件上传域"); // uploadFile.setSuccess(0);
saveUploadFileList.add(uploadFile); // uploadFile.setMessage("未包含文件上传域");
return saveUploadFileList; // saveUploadFileList.add(uploadFile);
// return saveUploadFileList;
} }
DiskFileItemFactory dff = new DiskFileItemFactory();//1创建工厂 DiskFileItemFactory dff = new DiskFileItemFactory();//1创建工厂
String savePath = getSaveFilePath(); String savePath = getSaveFilePath();
@ -69,12 +71,13 @@ public class FastDFSUploader extends Uploader {
saveUploadFileList = doUpload(savePath, iter); saveUploadFileList = doUpload(savePath, iter);
} }
} catch (IOException e) { } catch (Exception e) {
UploadFile uploadFile = new UploadFile(); throw new UploadGeneralException(e);
uploadFile.setSuccess(1); // UploadFile uploadFile = new UploadFile();
uploadFile.setMessage("未知错误"); // uploadFile.setSuccess(1);
saveUploadFileList.add(uploadFile); // uploadFile.setMessage("未知错误");
e.printStackTrace(); // saveUploadFileList.add(uploadFile);
// e.printStackTrace();
} }
log.info("结束上传"); log.info("结束上传");
@ -83,16 +86,14 @@ public class FastDFSUploader extends Uploader {
private List<UploadFile> doUpload(String savePath, Iterator<String> iter) throws IOException{ private List<UploadFile> doUpload(String savePath, Iterator<String> iter){
List<UploadFile> saveUploadFileList = new ArrayList<>(); List<UploadFile> saveUploadFileList = new ArrayList<>();
try { try {
MultipartFile multipartfile = this.request.getFile(iter.next()); MultipartFile multipartfile = this.request.getFile(iter.next());
boolean uploadResult = uploadFileChunk(multipartfile); uploadFileChunk(multipartfile);
if (!uploadResult) {
}
String timeStampName = getTimeStampName(); String timeStampName = getTimeStampName();
String originalName = multipartfile.getOriginalFilename(); String originalName = multipartfile.getOriginalFilename();
@ -122,7 +123,8 @@ public class FastDFSUploader extends Uploader {
} }
} catch (Exception e) { } catch (Exception e) {
log.error("上传出错:" + e); throw new UploadGeneralException(e);
// log.error("上传出错:" + e);
} }
uploadFile.setIsOSS(1); uploadFile.setIsOSS(1);
@ -132,7 +134,7 @@ public class FastDFSUploader extends Uploader {
return saveUploadFileList; return saveUploadFileList;
} }
public boolean uploadFileChunk(MultipartFile multipartFile) { public void uploadFileChunk(MultipartFile multipartFile) {
// 存储在fastdfs不带组的路径 // 存储在fastdfs不带组的路径
// String noGroupPath = ""; // String noGroupPath = "";
@ -150,7 +152,8 @@ public class FastDFSUploader extends Uploader {
// return false; // return false;
// } // }
if (lock != null && lock) { if (lock != null && lock) {
return false; throw new UploadGeneralException("请求块锁失败");
//return false;
} }
LOCK_MAP.put(uploadFile.getIdentifier(), true); LOCK_MAP.put(uploadFile.getIdentifier(), true);
// 写入锁的当前拥有者 // 写入锁的当前拥有者
@ -167,10 +170,12 @@ public class FastDFSUploader extends Uploader {
//此段代码保证顺序如果满足条件则返回失败 //此段代码保证顺序如果满足条件则返回失败
if (uploadFile.getChunkNumber() < currentChunkInRedis) { if (uploadFile.getChunkNumber() < currentChunkInRedis) {
log.info("当前文件块已上传"); log.info("当前文件块已上传");
return false; throw new UploadGeneralException("当前文件块已上传");
// return false;
} else if (uploadFile.getChunkNumber() > currentChunkInRedis) { } else if (uploadFile.getChunkNumber() > currentChunkInRedis) {
log.info("当前文件块需要等待上传,稍后请重试"); log.info("当前文件块需要等待上传,稍后请重试");
return false; throw new UploadGeneralException("当前文件块需要等待上传,稍后请重试");
// return false;
} }
log.info("***********开始上传第{}块**********", uploadFile.getChunkNumber()); log.info("***********开始上传第{}块**********", uploadFile.getChunkNumber());
@ -190,12 +195,14 @@ public class FastDFSUploader extends Uploader {
if (storePath == null) { if (storePath == null) {
CURRENT_UPLOAD_CHUNK_NUMBER.put(uploadFile.getIdentifier(), uploadFile.getChunkNumber()); CURRENT_UPLOAD_CHUNK_NUMBER.put(uploadFile.getIdentifier(), uploadFile.getChunkNumber());
log.info("获取远程文件路径出错"); log.info("获取远程文件路径出错");
return false; throw new UploadGeneralException("获取远程文件路径出错");
// return false;
} }
} catch (Exception e) { } catch (Exception e) {
CURRENT_UPLOAD_CHUNK_NUMBER.put(uploadFile.getIdentifier(), uploadFile.getChunkNumber()); CURRENT_UPLOAD_CHUNK_NUMBER.put(uploadFile.getIdentifier(), uploadFile.getChunkNumber());
log.error("初次上传远程文件出错", e); log.error("初次上传远程文件出错", e);
return false; throw new UploadGeneralException("初次上传远程文件出错", e);
// return false;
} }
STORE_PATH.put(uploadFile.getIdentifier(), storePath.getPath()); STORE_PATH.put(uploadFile.getIdentifier(), storePath.getPath());
@ -207,7 +214,8 @@ public class FastDFSUploader extends Uploader {
if (path == null) { if (path == null) {
log.error("无法获取已上传服务器文件地址"); log.error("无法获取已上传服务器文件地址");
CURRENT_UPLOAD_CHUNK_NUMBER.put(uploadFile.getIdentifier(), uploadFile.getChunkNumber()); CURRENT_UPLOAD_CHUNK_NUMBER.put(uploadFile.getIdentifier(), uploadFile.getChunkNumber());
return false; throw new UploadGeneralException("无法获取已上传服务器文件地址");
// return false;
} }
try { try {
Long alreadySize = UPLOADED_SIZE.get(uploadFile.getIdentifier()); Long alreadySize = UPLOADED_SIZE.get(uploadFile.getIdentifier());
@ -220,12 +228,14 @@ public class FastDFSUploader extends Uploader {
} catch (Exception e) { } catch (Exception e) {
CURRENT_UPLOAD_CHUNK_NUMBER.put(uploadFile.getIdentifier(), uploadFile.getChunkNumber()); CURRENT_UPLOAD_CHUNK_NUMBER.put(uploadFile.getIdentifier(), uploadFile.getChunkNumber());
log.error("更新远程文件出错", e); log.error("更新远程文件出错", e);
return false; throw new UploadGeneralException("更新远程文件出错", e);
// return false;
} }
} }
} catch (Exception e) { } catch (Exception e) {
log.error("上传文件错误", e); log.error("上传文件错误", e);
return false; throw new UploadGeneralException("上传文件错误", e);
// return false;
} }
} finally { } finally {
// 锁的当前拥有者才能释放块上传锁 // 锁的当前拥有者才能释放块上传锁
@ -235,6 +245,6 @@ public class FastDFSUploader extends Uploader {
} }
} }
log.info("***********第{}块上传成功**********", uploadFile.getChunkNumber()); log.info("***********第{}块上传成功**********", uploadFile.getChunkNumber());
return true; // return true;
} }
} }

View File

@ -28,7 +28,7 @@ public class TaskController {
@Scheduled(cron = "0 0/1 * * * ?") @Scheduled(cron = "0 0/1 * * * ?")
public void deleteFile() { public void deleteFile() {
log.info("111112");
LambdaQueryWrapper<FileBean> fileBeanLambdaQueryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<FileBean> fileBeanLambdaQueryWrapper = new LambdaQueryWrapper<>();
fileBeanLambdaQueryWrapper.eq(FileBean::getPointCount, 0); fileBeanLambdaQueryWrapper.eq(FileBean::getPointCount, 0);
@ -39,8 +39,7 @@ public class TaskController {
fileService.deleteLocalFile(fileBean); fileService.deleteLocalFile(fileBean);
fileService.removeById(fileBean.getFileId()); fileService.removeById(fileBean.getFileId());
} }
fileService.remove(fileBeanLambdaQueryWrapper); // fileService.remove(fileBeanLambdaQueryWrapper);
log.info("11111");
} }
} }