Java tutorial
/* * Copyright 2013 NingPai, Inc. All rights reserved. * NINGPAI PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package com.ylife.goods.model; import com.ylife.goods.mapper.SysHelperMapper; import com.ylife.goods.service.FastDFSInfoService; import org.apache.commons.fileupload.FileItem; import org.springframework.stereotype.Controller; import org.springframework.web.multipart.MultipartFile; import javax.annotation.PostConstruct; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.util.Arrays; import java.util.HashMap; import java.util.Map; /** * @author ggn */ @Controller public class UploadUtil { private static final String LOGGERINFO1 = "???"; private static final String IMAGE = "image"; /** DAO */ private static SysHelperMapper sysHelperMapper; /** DAO */ @Resource private SysHelperMapper sysHelperMapperNext; /** ? */ private static long maxSize = 10 * 1024 * 1024; /** ??? */ private static HashMap<String, String> extMap = new HashMap<String, String>(); /** * FastDFSInfoService ?? */ private static FastDFSInfoService fastDFSService; /** * FastDFSInfoService ?? */ @Resource private FastDFSInfoService fastDFSServiceNext; /** * ?? */ private static UploadImgUpyun uploadImgUpyun; /** Java */ private static UploadImgJava uploadImgJava; /** * ?FastDFS */ private static UploadImgFastDFS uploadImgFastDFS; /** * ?DAO */ @PostConstruct public void init() { sysHelperMapper = this.sysHelperMapperNext; fastDFSService = this.fastDFSServiceNext; } /** * key * * @param muFile * * @param request * HttpServletRequest * @return Map key{0,1,2,oldimg} 0? 1 2 oldimg */ public static synchronized Map<String, String> uploadFile(MultipartFile muFile, HttpServletRequest request) { Map<String, String> map = new HashMap<String, String>(); UpyunConf upConf = sysHelperMapper.selectUpyunConf(); FastDFSInfo fastdfs = fastDFSService.getFastDFSInfoByCurr(); if (muFile != null && muFile.getSize() != 0) { // ??? if (!checkFileForSpringUpload(muFile)) { throw new RuntimeException("" + muFile.getOriginalFilename() + LOGGERINFO1); } // ??? if (upConf != null && "1".equals(upConf.getUsedStatus())) { // ??? if (null == fastdfs || "0".equals(fastdfs.getUserd())) { map = uploadImgUpyun.uploadForABCSize(muFile, upConf); } else { // FastDFS map = uploadImgUpyun.uploadForABCSize(muFile, upConf); map = uploadImgFastDFS.saveToFastDFS(map, fastdfs); } } else { // Java? // ?? if (null == fastdfs || "0".equals(fastdfs.getUserd())) { map = uploadImgJava.uploadForABCSize(muFile); } else { // FastDFS map = uploadImgJava.uploadForABCSizeSaveFastDFS(muFile); map = uploadImgFastDFS.saveToFastDFS(map, fastdfs); } } return map; } else { return null; } } /** * ? * * @param muFile * * @param request * HttpServletRequest * @return String */ public static synchronized String uploadFileOne(MultipartFile muFile, HttpServletRequest request) { Map<String, String> map = new HashMap<String, String>(); UpyunConf upConf = sysHelperMapper.selectUpyunConf(); FastDFSInfo fastdfs = fastDFSService.getFastDFSInfoByCurr(); if (muFile != null && muFile.getSize() != 0) { // ??? if (!checkFileForSpringUpload(muFile)) { throw new RuntimeException("" + muFile.getOriginalFilename() + LOGGERINFO1); } // ??? if (upConf != null && "1".equals(upConf.getUsedStatus())) { // ??? if (null == fastdfs || "0".equals(fastdfs.getUserd())) { map = uploadImgUpyun.uploadForOldAndSmall(muFile, upConf); } else { // FastDFS map = uploadImgUpyun.uploadForOldAndSmall(muFile, upConf); map = uploadImgFastDFS.saveToFastDFS(map, fastdfs); } } else { // Java? // ?? if (null == fastdfs || "0".equals(fastdfs.getUserd())) { map = uploadImgJava.uploadForOldAndSmall(muFile); } else { // FastDFS map = uploadImgJava.uploadForOldAndSmallSaveFastDFS(muFile); map = uploadImgFastDFS.saveToFastDFS(map, fastdfs); } } return map.get("oldimg"); } else { return null; } } /** * ? * * @param muFile * * @param request * HttpServletRequest * @return String */ public static synchronized String uploadFileCustomerHeadOne(MultipartFile muFile, HttpServletRequest request) { Map<String, String> map = new HashMap<String, String>(); UpyunConf upConf = sysHelperMapper.selectUpyunConf(); FastDFSInfo fastdfs = fastDFSService.getFastDFSInfoByCurr(); if (muFile != null && muFile.getSize() != 0) { // ??? if (!checkFileForSpringUpload(muFile)) { throw new RuntimeException("" + muFile.getOriginalFilename() + LOGGERINFO1); } // ??? if (upConf != null && "1".equals(upConf.getUsedStatus())) { // ??? if (null == fastdfs || "0".equals(fastdfs.getUserd())) { map = uploadImgUpyun.uploadForOldAndSmall(muFile, upConf); } else { // FastDFS map = uploadImgUpyun.uploadForOldAndSmall(muFile, upConf); map = uploadImgFastDFS.saveToFastDFS(map, fastdfs); } } else { // Java? // ?? if (null == fastdfs || "0".equals(fastdfs.getUserd())) { map = uploadImgJava.uploadForOldAndSmall(muFile); } else { // FastDFS map = uploadImgJava.uploadForABCSizeSaveFastDFS(muFile); map = uploadImgFastDFS.saveToFastDFS(map, fastdfs); } } return map.get("oldimg"); } else { return null; } } /** * ,?key * * @param muFile * * @param request * HttpServletRequest * @return Map key{width...,oldimg} oldimg */ public static synchronized Map<String, String> uploadFileByWidth(MultipartFile muFile, HttpServletRequest request) { Map<String, String> map = new HashMap<String, String>(); UpyunConf upConf = sysHelperMapper.selectUpyunConf(); FastDFSInfo fastdfs = fastDFSService.getFastDFSInfoByCurr(); if (muFile != null && muFile.getSize() != 0) { // ??? if (!checkFileForSpringUpload(muFile)) { throw new RuntimeException("" + muFile.getOriginalFilename() + LOGGERINFO1); } // ??? if (upConf != null && "1".equals(upConf.getUsedStatus())) { // ??? if (null == fastdfs || "0".equals(fastdfs.getUserd())) { map = uploadImgUpyun.uploadForAllSize(muFile, upConf); } else { // FastDFS map = uploadImgUpyun.uploadForAllSize(muFile, upConf); map = uploadImgFastDFS.saveToFastDFS(map, fastdfs); } } else { // Java? // ?? if (null == fastdfs || "0".equals(fastdfs.getUserd())) { map = uploadImgJava.uploadForAllSize(muFile); } else { // FastDFS map = uploadImgJava.uploadForABCSizeSaveFastDFSWidth(muFile); map = uploadImgFastDFS.saveToFastDFS(map, fastdfs); } } return map; } else { return null; } } /** * Kindeditor * * @param item * FileItem * @param request * HttpServletRequest * @return ? */ public static synchronized String uploadFileOneByFile(FileItem item, HttpServletRequest request) { String resoult = ""; UpyunConf upConf = sysHelperMapper.selectUpyunConf(); FastDFSInfo fastdfs = fastDFSService.getFastDFSInfoByCurr(); if (item != null && item.getSize() != 0) { // ??? if (!checkFile(item)) { throw new RuntimeException("???"); } // ??? if (upConf != null && "1".equals(upConf.getUsedStatus())) { // ??? if (null == fastdfs || "0".equals(fastdfs.getUserd())) { resoult = uploadImgUpyun.uploadForRichEdit(item, upConf); } else { // FastDFS resoult = uploadImgUpyun.uploadForRichEdit(item, upConf); resoult = uploadImgFastDFS.saveToFastDFS(resoult, fastdfs); } } else { // Java? // ?? if (null == fastdfs || "0".equals(fastdfs.getUserd())) { resoult = uploadImgJava.uploadForRichEdit(item); } else { // FastDFS resoult = uploadImgJava.uploadForRichEditSaveFastDFS(item); resoult = uploadImgFastDFS.saveToFastDFS(resoult, fastdfs); } } return resoult; } else { return null; } } /** * Kindeditor * * @param item * FileItem * @param request * HttpServletRequest * @return ? */ public static synchronized String uploadFileOneByFileSite(FileItem item, HttpServletRequest request) { String resoult = ""; UpyunConf upConf = sysHelperMapper.selectUpyunConf(); FastDFSInfo fastdfs = fastDFSService.getFastDFSInfoByCurr(); if (item != null && item.getSize() != 0) { // ??? if (!checkFile(item)) { throw new RuntimeException("???"); } // ??? if (upConf != null && "1".equals(upConf.getUsedStatus())) { // ??? if (null == fastdfs || "0".equals(fastdfs.getUserd())) { resoult = uploadImgUpyun.uploadForRichEdit(item, upConf); } else { // FastDFS resoult = uploadImgUpyun.uploadForRichEdit(item, upConf); resoult = uploadImgFastDFS.saveToFastDFS(resoult, fastdfs); } } else { // Java? // ?? if (null == fastdfs || "0".equals(fastdfs.getUserd())) { resoult = uploadImgJava.uploadForRichEdit(item); } else { // FastDFS resoult = uploadImgJava.uploadForRichEditSaveFastDFS(item); resoult = uploadImgFastDFS.saveToFastDFS(resoult, fastdfs); } } return resoult; } else { return null; } } /** * ????,SpringMVC * * @param muFile * MultipartFile * @return ?? */ private static boolean checkFileForSpringUpload(MultipartFile muFile) { boolean bool = true; // ? if (muFile.getSize() > maxSize) { //LOGGER.error("=============>" + muFile.getOriginalFilename() + "??"); bool = false; } String fileName = muFile.getOriginalFilename(); // ?? String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase(); if (!Arrays.<String>asList(extMap.get(IMAGE).split(",")).contains(fileExt)) { //LOGGER.error("" + muFile.getOriginalFilename() + "??????\n??" + extMap.get(IMAGE) + "?"); bool = false; } return bool; } /** * ?? * * @param muFile * MultipartFile * @param request * HttpServletRequest * @return ???Map * @throws java.io.IOException * */ public static synchronized Map<String, String> testUpYun(MultipartFile muFile, HttpServletRequest request) throws IOException { Map<String, String> map = new HashMap<String, String>(); UpyunConf upConf = sysHelperMapper.selectUpyunConf(); if (muFile != null && muFile.getSize() != 0) { // ??? if (!checkFileForSpringUpload(muFile)) { throw new RuntimeException("" + muFile.getOriginalFilename() + LOGGERINFO1); } // ??? if (upConf != null) { // ??? map = uploadImgUpyun.testUpYun(muFile, upConf); } return map; } else { return null; } } /** * ????, * * @param file * FileItem * @return ?true?false */ private static boolean checkFile(FileItem file) { boolean bool = true; // ? if (file.getSize() > maxSize) { //LOGGER.error("=============>" + file.getFieldName() + "??"); bool = false; } String fileName = file.getName(); // ?? String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase(); if (!Arrays.<String>asList(extMap.get(IMAGE).split(",")).contains(fileExt)) { //LOGGER.error("" + file.getFieldName() + "??????\n??" + extMap.get(IMAGE) + "?"); bool = false; } return bool; } /** * ?UploadImgUpyun * * @return UploadImgUpyun */ public UploadImgUpyun getUploadImgUpyun() { return uploadImgUpyun; } /** * springUploadImgUpyun * * @param uploadImgUpyun * ?UploadImgUpyun */ @Resource(name = "UploadImgUpyun") public void setUploadImgUpyun(UploadImgUpyun uploadImgUpyun) { UploadUtil.uploadImgUpyun = uploadImgUpyun; } /** * ?UploadImgFastDFS * * @return UploadImgFastDFS */ public UploadImgFastDFS getUploadImgFastDFS() { return uploadImgFastDFS; } /** * spring uploadImgFastDFS * * @param uploadImgFastDFS * ?uploadImgFastDFS */ @Resource(name = "UploadImgFastDFS") public void setUploadImgFastDFS(UploadImgFastDFS uploadImgFastDFS) { UploadUtil.uploadImgFastDFS = uploadImgFastDFS; } /** * ?UploadImgJava * * @return UploadImgJava */ public UploadImgJava getUploadImgJava() { return uploadImgJava; } /** * springUploadImgJava * * @param uploadImgJava * ?UploadImgJava */ @Resource(name = "UploadImgJava") public void setUploadImgJava(UploadImgJava uploadImgJava) { UploadUtil.uploadImgJava = uploadImgJava; } // ? static { extMap.put(IMAGE, "gif,jpg,jpeg,png,bmp,ico"); } }