管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2022-10-18 ac93c3a1bc1e759d4f40566fc2201ce7c480ee96
1
已添加1个文件
已修改2个文件
253 ■■■■■ 文件已修改
src/main/java/com/lf/server/entity/ctrl/FileInfo.java 54 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/lf/server/helper/PathHelper.java 78 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/lf/server/service/data/UploaderService.java 121 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/lf/server/entity/ctrl/FileInfo.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,54 @@
package com.lf.server.entity.ctrl;
/**
 * æ–‡ä»¶ä¿¡æ¯
 * @author WWW
 */
public class FileInfo {
    private String fileName;
    private Long size;
    private String md5;
    private String path;
    public FileInfo() {
    }
    public FileInfo(String fileName) {
        this.fileName = fileName;
    }
    public String getFileName() {
        return fileName;
    }
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
    public Long getSize() {
        return size;
    }
    public void setSize(Long size) {
        this.size = size;
    }
    public String getMd5() {
        return md5;
    }
    public void setMd5(String md5) {
        this.md5 = md5;
    }
    public String getPath() {
        return path;
    }
    public void setPath(String path) {
        this.path = path;
    }
}
src/main/java/com/lf/server/helper/PathHelper.java
@@ -2,10 +2,13 @@
import com.lf.server.config.PropertiesConfig;
import com.lf.server.entity.all.SettingData;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.File;
import java.util.Date;
/**
 * è·¯å¾„帮助类
@@ -23,6 +26,10 @@
    private static int uploadPath = 1;
    private static int sharePath = 1;
    private final static double D92 = 92;
    private final static Log log = LogFactory.getLog(PathHelper.class);
    public PropertiesConfig getConfig() {
        return config;
@@ -75,8 +82,6 @@
    /**
     * èŽ·å–å¯¼å…¥ç›®å½•
     *
     * @return
     */
    public String getImportFullPath() {
        importPath = getSubPath(config.getImportPath(), importPath);
@@ -86,8 +91,6 @@
    /**
     * èŽ·å–å‡ºå›¾ç›®å½•
     *
     * @return
     */
    public String getExportFullPath() {
        exportPath = getSubPath(config.getExportPath(), exportPath);
@@ -97,8 +100,6 @@
    /**
     * èŽ·å–ä¸Šä¼ ç›®å½•
     *
     * @return
     */
    public String getUploadFullPath() {
        uploadPath = getSubPath(config.getUploadPath(), uploadPath);
@@ -108,12 +109,73 @@
    /**
     * èŽ·å–å…±äº«ç›®å½•
     *
     * @return
     */
    public String getShareFullPath() {
        sharePath = getSubPath(config.getSharePath(), sharePath);
        return config.getSharePath() + File.separator + sharePath;
    }
    /**
     * èŽ·å–ä¸´æ—¶è·¯å¾„
     */
    public String getTempPath() {
        String tempName = FileHelper.getTempPath();
        String tempPath = config.getTempPath();
        String path = tempPath + File.separator + tempName;
        File file = new File(path);
        if (!file.exists() && !file.isDirectory()) {
            file.mkdirs();
        }
        double ran = Math.random() * 99;
        if (ran > D92) {
            deleteOldPath(tempPath);
        }
        return path;
    }
    /**
     * åˆ é™¤æ—§è·¯å¾„
     */
    public void deleteOldPath(String tempPath) {
        try {
            File file = new File(tempPath);
            String str = StringHelper.YMD__FORMAT.format(new Date());
            File[] files = file.listFiles();
            for (File f : files) {
                if (f.getPath().indexOf(str) > -1) {
                    continue;
                }
                deleteFiles(f);
            }
        } catch (Exception ex) {
            log.error(ex.getMessage() + ex.getStackTrace() + "\n");
        }
    }
    /**
     * çº§è”删除文件
     */
    public void deleteFiles(File file) {
        if (file == null || !file.exists()) {
            return;
        }
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File f : files) {
                if (f.isDirectory()) {
                    deleteFiles(f);
                } else {
                    f.delete();
                }
            }
        }
        file.delete();
    }
}
src/main/java/com/lf/server/service/data/UploaderService.java
@@ -5,6 +5,7 @@
import com.lf.server.entity.all.HttpStatus;
import com.lf.server.entity.all.ResponseMsg;
import com.lf.server.entity.all.SettingData;
import com.lf.server.entity.ctrl.FileInfo;
import com.lf.server.entity.sys.AttachEntity;
import com.lf.server.entity.sys.UserEntity;
import com.lf.server.helper.FileHelper;
@@ -31,9 +32,7 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.*;
/**
 * æ•°æ®ä¸Šä¼ æœåŠ¡ç±»
@@ -46,8 +45,6 @@
    @Autowired
    AttachService attachService;
    private final static double D92 = 92;
    private final static long ONE_DAY = 24 * 60 * 60 * 1000;
@@ -73,7 +70,7 @@
            // ä¼ è¾“文件
            String oldName = file.getOriginalFilename();
            String filePath = pathHelper.getConfig().getTempPath() + File.separator + oldName;
            String filePath = pathHelper.getTempPath() + File.separator + oldName;
            File newFile = new File(filePath);
            file.transferTo(newFile);
@@ -207,64 +204,66 @@
            // list容器用来保存表单中的所有数据信息
            List<FileItem> items = sfu.parseRequest(ctx);
            // éåŽ†å®¹å™¨ï¼Œå¤„ç†è§£æžçš„å†…å®¹ï¼šä¸€ä¸ªå¤„ç†æ™®é€šè¡¨å•åŸŸï¼Œä¸€ä¸ªå¤„ç†æ–‡ä»¶çš„è¡¨å•åŸŸ
            for (FileItem item : items) {
                if (item.isFormField()) {
                    handleFormField(item);
                } else {
                    handleFileField(item, req);
                }
            copeFileItems(items, req);
        } catch (Exception ex) {
            log.error(ex.getMessage() + ex.getStackTrace() + "\n");
        }
    }
    /**
     * å¤„理解析内容:处理普通表单域和文件表单域
     */
    private void copeFileItems(List<FileItem> items, HttpServletRequest req) throws Exception {
        Map<String, String> map = new HashMap<String, String>(3);
        List<FileInfo> list = new ArrayList<FileInfo>();
        String path = pathHelper.getTempPath();
        for (FileItem item : items) {
            if (item.isFormField()) {
                String key = item.getFieldName();
                String value = item.getString("utf-8");
                map.put(key, value);
                continue;
            }
        } catch (Exception ex) {
            log.error(ex.getMessage() + ex.getStackTrace() + "\n");
            FileInfo fi = copeFile(item, path, req);
            if (fi != null) {
                list.add(fi);
            }
        }
    }
    /**
     * å¤„理 æ™®é€šæ•°æ®é¡¹
     *
     * @param item
     * å¤„理文件
     */
    private void handleFormField(FileItem item) {
        // èŽ·å– æ™®é€šæ•°æ®é¡¹ä¸­çš„ name值
        String fieldName = item.getFieldName();
        // èŽ·å– æ™®é€šæ•°æ®é¡¹ä¸­çš„ value值
        String value = "";
    private FileInfo copeFile(FileItem item, String path, HttpServletRequest req) {
        try {
            // ä»¥ utf-8的编码格式来解析 value值
            value = item.getString("utf-8");
            // èŽ·å–æ–‡ä»¶åï¼Œåˆ¤æ–­æ˜¯å¦åˆæ³•
            FileInfo fi = new FileInfo(item.getName());
            if (StringHelper.isEmpty(fi.getFileName())) {
                return null;
            }
            fi.setSize(item.getSize());
            fi.setPath(path + File.separator + fi.getFileName());
            item.write(new File(fi.getPath()));
            return fi;
        } catch (Exception ex) {
            log.error(ex.getMessage() + ex.getStackTrace() + "\n");
            return null;
        }
        // è¾“出到控制台
        System.out.println("fieldName:" + fieldName + "--value:" + value);
    }
    /**
     * å¤„理 æ–‡ä»¶æ•°æ®é¡¹
     *
     * @param item
     */
    private void handleFileField(FileItem item, HttpServletRequest req) {
        // èŽ·å– æ–‡ä»¶æ•°æ®é¡¹ä¸­çš„ æ–‡ä»¶å
        String fileName = item.getName();
        // åˆ¤æ–­ æ­¤æ–‡ä»¶çš„æ–‡ä»¶åæ˜¯å¦åˆæ³•
        if (fileName == null || "".equals(fileName)) {
            return;
        }
        // æŽ§åˆ¶åªèƒ½ä¸Šä¼ å›¾ç‰‡
        String img = "image";
        if (!item.getContentType().startsWith(img)) {
            return;
        }
        // å°†æ–‡ä»¶ä¿¡æ¯è¾“出到控制台:文件名+文件大小
        System.out.println("fileName:" + fileName);
        System.out.println("fileSize:" + item.getSize());
        // èŽ·å– å½“前项目下的 /files ç›®å½•的绝对位置
        ServletContext ctx = req.getSession().getServletContext();
@@ -282,42 +281,6 @@
            item.write(new File(file.toString(), UUID.randomUUID() + "_" + fileName));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * èŽ·å–ä¸´æ—¶è·¯å¾„
     */
    public String getTempPath() {
        String tempName = FileHelper.getTempPath();
        String tempPath = pathHelper.getConfig().getTempPath();
        String path = tempPath + File.separator + tempName;
        File file = new File(path);
        if (!file.exists() && !file.isDirectory()) {
            file.mkdirs();
        }
        double ran = Math.random() * 99;
        if (ran > D92) {
            deleteOldPath();
        }
        return path;
    }
    public void deleteOldPath() {
        try {
            Long time = System.currentTimeMillis();
            for (int i = 1, c = 30; i < c; i++) {
                Date date = new Date(time - i * ONE_DAY);
                String str = StringHelper.YMD__FORMAT.format(date);
                //
            }
        } catch (Exception ex) {
            log.error(ex.getMessage() + ex.getStackTrace() + "\n");
        }
    }
}