package com.matrix.biz.action; 
 | 
  
 | 
import com.alibaba.fastjson.JSONObject; 
 | 
import com.matrix.core.tools.LogUtil; 
 | 
import com.matrix.core.tools.StringUtils; 
 | 
import com.matrix.core.tools.UUIDUtil; 
 | 
import com.matrix.system.common.constance.AppConstance; 
 | 
import org.apache.commons.fileupload.FileUploadException; 
 | 
import org.apache.log4j.Logger; 
 | 
import org.springframework.beans.factory.annotation.Value; 
 | 
import org.springframework.stereotype.Controller; 
 | 
import org.springframework.util.FileCopyUtils; 
 | 
import org.springframework.web.bind.annotation.RequestMapping; 
 | 
import org.springframework.web.bind.annotation.ResponseBody; 
 | 
import org.springframework.web.multipart.MultipartFile; 
 | 
import org.springframework.web.multipart.MultipartHttpServletRequest; 
 | 
  
 | 
import javax.servlet.http.HttpServletResponse; 
 | 
import java.io.File; 
 | 
import java.io.IOException; 
 | 
import java.text.SimpleDateFormat; 
 | 
import java.util.Date; 
 | 
import java.util.Map; 
 | 
import java.util.regex.Matcher; 
 | 
import java.util.regex.Pattern; 
 | 
  
 | 
/** 
 | 
 * 多文件上传控制器 
 | 
 *  
 | 
 * @author jiangyouyao 
 | 
 * @email 512061637@qq.com 
 | 
 * @date 2019年2月25日 
 | 
 */ 
 | 
@Controller 
 | 
@RequestMapping(value = "admin/multipleUploadFile") 
 | 
public class MultipleFileUploadAction { 
 | 
    Logger log = Logger.getLogger(MultipleFileUploadAction.class); 
 | 
  
 | 
    @Value("${file_storage_path}") 
 | 
    private String fileStoragePath; 
 | 
    @Value("${static_resource_url}") 
 | 
    private String nginxUrl; 
 | 
  
 | 
    /** 
 | 
     * 最大值 
 | 
     */ 
 | 
    private Long maxSize = 1024*1024*100L; 
 | 
  
 | 
    /** 
 | 
     * 多文件上传方法 
 | 
     *  
 | 
     * @author jiangyouyao 
 | 
     * @email 512061637@qq.com 
 | 
     * @date 2019年2月25日 
 | 
     * @param response 
 | 
     * @param request 
 | 
     * @return 
 | 
     * @throws IOException 
 | 
     * @throws FileUploadException 
 | 
     */ 
 | 
    @RequestMapping(value = "/doUpload") 
 | 
    public @ResponseBody JSONObject doFileUpload(HttpServletResponse response, MultipartHttpServletRequest request, Integer data) 
 | 
            throws IOException, FileUploadException { 
 | 
        // 文件保存目录路径 
 | 
        String savePath = fileStoragePath; 
 | 
        // 文件保存目录URL 
 | 
        String saveUrl = nginxUrl; 
 | 
        // String msgPag = "common/fileUploadResult"; 
 | 
        JSONObject object = new JSONObject(); 
 | 
        response.setContentType("text/html; charset=UTF-8"); 
 | 
        request.setCharacterEncoding("UTF-8"); 
 | 
  
 | 
        // 保存和访问路径检查 
 | 
        if (StringUtils.isBlank(saveUrl) || StringUtils.isBlank(savePath)) { 
 | 
            object.put("status", "err"); 
 | 
            object.put("msg", "文件上传失败错误代码:001"); 
 | 
            return object; 
 | 
        } 
 | 
        // 检查目录 
 | 
        File uploadDir = new File(savePath); 
 | 
        if (!uploadDir.isDirectory()) { 
 | 
            uploadDir.mkdir(); 
 | 
        } 
 | 
        // 检查目录写权限 
 | 
//        if (!uploadDir.canWrite()) { 
 | 
//            object.put("status", "err"); 
 | 
//            object.put("msg", "上传目录没有写权限"); 
 | 
//            return object; 
 | 
//        } 
 | 
  
 | 
        Map<String, MultipartFile> fileMaps = request.getFileMap(); 
 | 
        for (String key : fileMaps.keySet()) { 
 | 
            MultipartFile file = fileMaps.get(key); 
 | 
  
 | 
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); 
 | 
            String ymd = sdf.format(new Date()); 
 | 
            savePath += ymd + "/"; 
 | 
            saveUrl += ymd + "/"; 
 | 
            File dirFile = new File(savePath); 
 | 
            if (!dirFile.exists()) { 
 | 
                dirFile.mkdirs(); 
 | 
            } 
 | 
            log.info("上传文件名:" + file.getOriginalFilename()); 
 | 
            log.info("上传文件大小:" + file.getBytes().length); 
 | 
            log.info("上传文件大小限制:" + maxSize); 
 | 
            log.info("上传文件大小是否超过限制:" + (file.getBytes().length > maxSize)); 
 | 
            if (file.getBytes().length > maxSize) { 
 | 
                object.put("status", "err"); 
 | 
                object.put("msg", "上传文件大小超过限制"); 
 | 
                return object; 
 | 
            } 
 | 
            String fileName = file.getOriginalFilename(); 
 | 
            String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase(); 
 | 
  
 | 
            fileName = fileName.replace("." + fileExt, ""); 
 | 
            fileName = getSensitive(fileName); 
 | 
            String newFileName = UUIDUtil.getRandomID() + UUIDUtil.getRandomID() + "." + fileExt; 
 | 
            File uploadedFile = new File(savePath, newFileName); 
 | 
            try { 
 | 
                FileCopyUtils.copy(file.getBytes(), uploadedFile); 
 | 
            } catch (Exception e) { 
 | 
                object.put("status", "err"); 
 | 
                object.put("msg", "上传文件失败 "+e.getMessage()); 
 | 
                return object; 
 | 
            } 
 | 
            log.info("saveUrl:" + saveUrl); 
 | 
            String visitPath = saveUrl + newFileName; 
 | 
            log.info("上传一个文件:" + newFileName); 
 | 
            log.info("访问路径:" + visitPath); 
 | 
            // 获取回调函数 
 | 
            /* 
 | 
             * String callBack = request.getParameter("callBack"); String inputId = 
 | 
             * request.getParameter("inputId"); request.setAttribute("status", "200"); 
 | 
             * request.setAttribute("callBack", callBack); request.setAttribute("inputId", 
 | 
             * inputId); request.setAttribute("url", visitPath); 
 | 
             */ 
 | 
            object.put("path", visitPath); 
 | 
            object.put("fileName", fileName); 
 | 
            object.put("status", 200); 
 | 
            if (data != null) { 
 | 
                object.put("index", data); 
 | 
            } 
 | 
        } 
 | 
        return object; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 检查文件名,过滤特殊字符 
 | 
     *  
 | 
     * @author jiangyouyao 
 | 
     * @email 512061637@qq.com 
 | 
     * @date 2019年2月25日 
 | 
     * @param globWords 
 | 
     * @return 
 | 
     */ 
 | 
    public String getSensitive(String globWords) { 
 | 
  
 | 
        String sensitive = ""; 
 | 
        Pattern pattern = Pattern.compile(AppConstance.SPECIAL_CHARACTERS); 
 | 
        Matcher matcher = pattern.matcher(globWords); 
 | 
        while (matcher.find()) { 
 | 
            sensitive += matcher.group(); 
 | 
        } 
 | 
        /* 
 | 
         * if(sensitive=="" || sensitive.length()<3 ){ 
 | 
         * sensitive=StringUtils.getRandomString(8); } 
 | 
         */ 
 | 
        return sensitive; 
 | 
    } 
 | 
} 
 |