管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2022-10-18 09b4103133939c8562e1768018ad6d4e6f0d67c8
src/main/java/com/lf/server/service/all/FileService.java
@@ -4,6 +4,7 @@
import com.lf.server.controller.all.BaseController;
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.sys.AttachEntity;
import com.lf.server.entity.sys.UserEntity;
import com.lf.server.helper.FileHelper;
@@ -30,6 +31,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;
@@ -45,7 +47,13 @@
    @Autowired
    AttachService attachService;
    private static long MAX_FILE_SIZE = 20 * 1024 * 1024;
    private final static double D92 = 92;
    private final static long ONE_DAY = 24 * 60 * 60 * 1000;
    private final static long SIZE_MAX = 1024 * 1024 * 1024 * 1024;
    private final static long FILE_SIZE_MAX = 256 * 1024 * 1024 * 1024;
    private final static Log log = LogFactory.getLog(FileService.class);
@@ -59,8 +67,8 @@
            if (file == null && file.isEmpty()) {
                return ctrl.fail("文件上传为空", null);
            }
            if (file.getSize() > MAX_FILE_SIZE) {
                return ctrl.fail(String.format("文件大于 %d MB", MAX_FILE_SIZE / 1024 / 1024), null);
            if (file.getSize() > SettingData.MAX_FILE_SIZE) {
                return ctrl.fail(String.format("文件大于 %d MB", SettingData.MAX_FILE_SIZE / 1024 / 1024), null);
            }
            // 传输文件
@@ -173,19 +181,16 @@
    /**
     * 上传文件
     *
     * @param req
     * @param res
     */
    public void upload(HttpServletRequest req, HttpServletResponse res) {
    public void uploadData(HttpServletRequest req, HttpServletResponse res) {
        try {
            // 处理中文乱码问题
            req.setCharacterEncoding("utf-8");
            res.setContentType("text/html;charset=utf-8");
            // 检查请求是/否是multipart/form-data类型
            // 检查请求是/否为multipart/form-data类型
            if (!ServletFileUpload.isMultipartContent(req)) {
                throw new RuntimeException("表单的enctype属性不是multipart/form-data类型!!");
                throw new RuntimeException("表单的enctype属性不是multipart/form-data类型");
            }
            // 创建上传所需要的两个对象:磁盘文件对象+文件上传对象
@@ -193,18 +198,15 @@
            ServletFileUpload sfu = new ServletFileUpload(factory);
            ServletRequestContext ctx = new ServletRequestContext(req);
            //限制单个文件的大小
            sfu.setFileSizeMax(1024 * 10);
            //限制上传的总文件大小
            sfu.setSizeMax(1024 * 200);
            // 限制上传的总文件大小
            sfu.setSizeMax(SIZE_MAX);
            // 限制单个文件的大小
            sfu.setFileSizeMax(FILE_SIZE_MAX);
            // 设置编码方式
            sfu.setHeaderEncoding("utf-8");
            // list容器用来保存表单中的所有数据信息
            List<FileItem> items = sfu.parseRequest(ctx);
            // 遍历容器,处理解析的内容:一个处理普通表单域,一个处理文件的表单域
            for (FileItem item : items) {
                if (item.isFormField()) {
@@ -282,4 +284,40 @@
            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");
        }
    }
}