All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.leazxl.cms.service.ueditor.upload.BinaryMVCUploader Maven / Gradle / Ivy

There is a newer version: 1.0.3
Show newest version
package com.leazxl.cms.service.ueditor.upload;

import com.leazxl.cms.service.ueditor.PathFormat;
import com.leazxl.cms.service.ueditor.define.BaseState;
import com.leazxl.cms.service.ueditor.define.FileType;
import com.leazxl.cms.service.ueditor.define.State;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
public class BinaryMVCUploader {

    /**
     * 保存文件
     *
     * @author lance
     * 2015年8月27日 下午2:26:49
     */
    public static final State save(HttpServletRequest request, Map conf) {
        boolean isAjaxUpload = request.getHeader("X_Requested_With") != null;
        if (!ServletFileUpload.isMultipartContent(request)) {
            return new BaseState(false, 5);
        }
        State storageState = new BaseState(Boolean.TRUE);
        long maxSize = ((Integer) conf.get("maxSize")).longValue();
        try {

            String savePath = (String) conf.get("savePath");
            String filedName = (String) conf.get("fieldName");
            List files = ((MultipartHttpServletRequest) request).getFiles(filedName);
            String newFileName = "", fileExt = "", originFileName = "";
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
            for (MultipartFile file : files) {
                if (file != null) {
                    originFileName = file.getOriginalFilename();
                    fileExt = FileType.getSuffixByFilename(originFileName);
                    //新文件名称
                    if (!validType(fileExt, (String[]) conf.get("allowFiles"))) {
                        return new BaseState(false, 8);
                    }
                    try {
                        newFileName = PathFormat.parse(savePath, originFileName) + fileExt;
                        //上传文件
                        String physicalPath = (String) conf.get("rootPath") + newFileName;
                        storageState = StorageManager.saveFileByInputStream(file.getInputStream(),
                                physicalPath, maxSize);
                        //file.transferTo(new File(savePath + physicalPath));
                        if (storageState.isSuccess()) {
                            storageState.putInfo("url", newFileName);
                            storageState.putInfo("type", fileExt);
                            storageState.putInfo("original", originFileName + fileExt);
                        }
                    } catch (IllegalStateException | IOException e) {
                        log.error("百度上传附件保存文件错误: {}", e.getMessage());
                    }
                }
            }
            return storageState;
        } catch (Exception e) {
            log.error("百度上传附件文件错误: {}", e.getMessage());
        }
        return new BaseState(false, 4);
    }

    private static boolean validType(String type, String[] allowTypes) {
        List list = Arrays.asList(allowTypes);
        return list.contains(type);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy