|
|
@@ -0,0 +1,167 @@
|
|
|
+package space.core.apps.sys.service;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Locale;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+import org.apache.commons.fileupload.FileItem;
|
|
|
+import org.apache.commons.fileupload.FileUploadBase;
|
|
|
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
|
|
+import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
|
|
+
|
|
|
+import space.common.exception.SpaceRuntimeException;
|
|
|
+import space.core.apps.common.BaseTransaction;
|
|
|
+import space.core.apps.sys.utils.UploadFileUtil;
|
|
|
+import space.core.common.CommonVars;
|
|
|
+import space.core.common.utils.GsonUtil;
|
|
|
+import space.core.common.utils.ThumbnailatorUtil;
|
|
|
+
|
|
|
+public class UploadService extends BaseTransaction {
|
|
|
+ private static final long serialVersionUID = 1L;
|
|
|
+
|
|
|
+ private static final long IMAGE_MAX_SIZE = 20L * 1024L * 1024L;
|
|
|
+ private static final long VIDEO_MAX_SIZE = 1024L * 1024L * 1024L;
|
|
|
+ private static final long DEFAULT_MAX_SIZE = 10L * 1024L * 1024L;
|
|
|
+
|
|
|
+ public void ajaxUpload() throws IOException {
|
|
|
+ Map<String, Object> result = new HashMap<String, Object>();
|
|
|
+
|
|
|
+ try {
|
|
|
+ DiskFileItemFactory factory = new DiskFileItemFactory();
|
|
|
+ File tempDir = new File(CommonVars.getWebRootPath() + "/web/upload/modules/sys/temp/");
|
|
|
+ if (!tempDir.exists()) {
|
|
|
+ tempDir.mkdirs();
|
|
|
+ }
|
|
|
+ factory.setSizeThreshold(40960);
|
|
|
+ factory.setRepository(tempDir);
|
|
|
+
|
|
|
+ ServletFileUpload upload = new ServletFileUpload(factory);
|
|
|
+ upload.setHeaderEncoding("UTF-8");
|
|
|
+ upload.setFileSizeMax(VIDEO_MAX_SIZE);
|
|
|
+
|
|
|
+ List<FileItem> items = upload.parseRequest(getRequest());
|
|
|
+ List<Map<String, String>> data = new ArrayList<Map<String, String>>();
|
|
|
+ String uploadPath = UploadFileUtil.getUploadFilePath(getWebApp(), "files");
|
|
|
+ File uploadDir = new File(CommonVars.getWebRootPath() + uploadPath);
|
|
|
+ if (!uploadDir.exists()) {
|
|
|
+ uploadDir.mkdirs();
|
|
|
+ }
|
|
|
+
|
|
|
+ for (FileItem item : items) {
|
|
|
+ if (!item.isFormField()) {
|
|
|
+ data.add(uploadForAjax(item, uploadPath));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ result.put("code", "success");
|
|
|
+ result.put("msg", "上传成功");
|
|
|
+ result.put("data", data);
|
|
|
+ getWriter().write(GsonUtil.toJson(result));
|
|
|
+ } catch (SpaceRuntimeException e) {
|
|
|
+ result.put("code", "fail");
|
|
|
+ result.put("msg", e.getMessage());
|
|
|
+ getWriter().write(GsonUtil.toJson(result));
|
|
|
+ } catch (FileUploadBase.FileSizeLimitExceededException e) {
|
|
|
+ result.put("code", "fail");
|
|
|
+ result.put("msg", "单个文件上传大小不能超过1G,请压缩后重新上传");
|
|
|
+ getWriter().write(GsonUtil.toJson(result));
|
|
|
+ } catch (Exception e) {
|
|
|
+ result.put("code", "fail");
|
|
|
+ result.put("msg", "上传失败");
|
|
|
+ getWriter().write(GsonUtil.toJson(result));
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, String> uploadForAjax(FileItem item, String uploadPath) throws Exception {
|
|
|
+ Map<String, String> result = new HashMap<String, String>();
|
|
|
+ String originalName = normalizeFileName(item.getName());
|
|
|
+ long fileSize = item.getSize();
|
|
|
+ validateUploadFile(originalName, item.getContentType(), fileSize);
|
|
|
+
|
|
|
+ String suffix = getSuffix(originalName);
|
|
|
+ String targetPath = uploadPath + UUID.randomUUID().toString().replace("-", "") + suffix;
|
|
|
+ File targetFile = new File(CommonVars.getWebRootPath() + targetPath);
|
|
|
+ item.write(targetFile);
|
|
|
+
|
|
|
+ compressImage(targetFile);
|
|
|
+
|
|
|
+ result.put("code", "success");
|
|
|
+ result.put("msg", targetPath);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void validateUploadFile(String fileName, String contentType, long fileSize) throws SpaceRuntimeException {
|
|
|
+ if (fileName.length() == 0 || fileSize <= 0) {
|
|
|
+ throw new SpaceRuntimeException("请选择有效的上传文件");
|
|
|
+ }
|
|
|
+
|
|
|
+ String lowerName = fileName.toLowerCase(Locale.ENGLISH);
|
|
|
+ if (lowerName.matches(".*\\.(jsp|jspx|html|php|asp|aspx|exe)$")) {
|
|
|
+ throw new SpaceRuntimeException("该文件类型不允许上传");
|
|
|
+ }
|
|
|
+
|
|
|
+ long maxSize = DEFAULT_MAX_SIZE;
|
|
|
+ String fileType = "文件";
|
|
|
+ String maxSizeText = "10M";
|
|
|
+ if (isImageFile(lowerName, contentType)) {
|
|
|
+ maxSize = IMAGE_MAX_SIZE;
|
|
|
+ fileType = "图片";
|
|
|
+ maxSizeText = "20M";
|
|
|
+ } else if (isVideoFile(lowerName, contentType)) {
|
|
|
+ maxSize = VIDEO_MAX_SIZE;
|
|
|
+ fileType = "视频";
|
|
|
+ maxSizeText = "1G";
|
|
|
+ }
|
|
|
+
|
|
|
+ if (fileSize > maxSize) {
|
|
|
+ throw new SpaceRuntimeException(fileType + "上传大小不能超过" + maxSizeText + ",请压缩后重新上传");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String normalizeFileName(String fileName) {
|
|
|
+ if (fileName == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ String normalized = fileName.replaceAll("\\s+", "_");
|
|
|
+ int slashIndex = Math.max(normalized.lastIndexOf("\\"), normalized.lastIndexOf("/"));
|
|
|
+ if (slashIndex >= 0 && slashIndex + 1 < normalized.length()) {
|
|
|
+ normalized = normalized.substring(slashIndex + 1);
|
|
|
+ }
|
|
|
+ return normalized;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getSuffix(String fileName) {
|
|
|
+ int dotIndex = fileName.lastIndexOf(".");
|
|
|
+ if (dotIndex < 0 || dotIndex == fileName.length() - 1) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ return fileName.substring(dotIndex);
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean isImageFile(String lowerName, String contentType) {
|
|
|
+ return startsWithIgnoreCase(contentType, "image/")
|
|
|
+ || lowerName.matches(".*\\.(jpg|jpeg|png|gif|bmp|webp|svg)$");
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean isVideoFile(String lowerName, String contentType) {
|
|
|
+ return startsWithIgnoreCase(contentType, "video/")
|
|
|
+ || lowerName.matches(".*\\.(mp4|webm|ogg|ogv|mov|avi|wmv|flv|mkv|mpeg|mpg|rm|rmvb)$");
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean startsWithIgnoreCase(String value, String prefix) {
|
|
|
+ return value != null && value.toLowerCase(Locale.ENGLISH).startsWith(prefix);
|
|
|
+ }
|
|
|
+
|
|
|
+ void compressImage(File file) throws IOException {
|
|
|
+ if (!ThumbnailatorUtil.isImage(file)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ ThumbnailatorUtil.compress(file);
|
|
|
+ }
|
|
|
+}
|