Java tutorial
/* * * Copyright 2010-2013 NingPai, Inc. All rights reserved. * NINGPAI PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package com.ylife.goods.model; import com.upyun.UpYunUtil; import com.upyun.YunBean; import com.ylife.goods.mapper.ImageSetMapper; import org.apache.commons.fileupload.FileItem; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; /** * -?? * * @author NINGPAI-zhangqiang * @since 20131230 ?2:24:18 * @version 1.0 */ @Component("UploadImgUpyun") public class UploadImgUpyun { private static final int SMALL = 56; private static final String OLDIMG = "oldimg"; private static final String LOGGERINFO1 = "==========?"; private static final String LOGGERINFO2 = "=========="; private static final String LOGGERINFO3 = "?"; private static final String LOGGERINFO4 = "key=>"; @Resource private ImageSetMapper imageSetMapper; /** * ???? * * @param muFile * @return Map?4oldimg0??1?2 */ public Map<String, String> uploadForABCSize(MultipartFile muFile, UpyunConf upConf) { Map<String, String> imgMap = new HashMap<String, String>(); if (muFile != null && muFile.getSize() != 0) { // ??java? @SuppressWarnings("unused") boolean flag = false; // ??????????? String fileNamess = UploadImgCommon.getPicNamePathSuffix(); File file = new File(fileNamess); try { muFile.transferTo(file); if (upConf != null) { YunBean yb = new YunBean(); yb.setBucketName(upConf.getBucketName()); yb.setPassword(upConf.getPassWord()); yb.setUserName(upConf.getUserName()); UpYunUtil.yunUp(fileNamess, UploadImgCommon.prefix, yb, UploadImgCommon.suffix); // ?? //LOGGER.debug(LOGGERINFO1 + upConf.getUrlPath() + UploadImgCommon.prefix + UploadImgCommon.suffix); imgMap.put(OLDIMG, upConf.getUrlPath() + UploadImgCommon.prefix + UploadImgCommon.suffix); // ??85 int[] widths = UploadImgCommon.getImgSetOut85(imageSetMapper.queryImageSet()); UploadImgCommon.sortWidth(widths); for (int i = 0; i < widths.length; i++) { // ?? //LOGGER.debug(LOGGERINFO2 + widths[i] + LOGGERINFO3 + upConf.getUrlPath() + UploadImgCommon.prefix + UploadImgCommon.suffix + "!" + widths[i]); imgMap.put(i + "", upConf.getUrlPath() + UploadImgCommon.prefix + UploadImgCommon.suffix + "!" + widths[i]); } } else { flag = true; } } catch (IllegalStateException e) { //LOGGER.error(LOGGERINFO4, e); flag = true; } catch (IOException e) { //LOGGER.error(LOGGERINFO4, e); flag = true; } catch (Exception e) { // LOGGER.error(LOGGERINFO4, e); flag = true; } return imgMap; } else { return null; } } /** * ???? * * @param muFile * @return Map?key */ public Map<String, String> uploadForAllSize(MultipartFile muFile, UpyunConf upConf) { Map<String, String> imgMap = new HashMap<String, String>(); if (muFile != null && muFile.getSize() != 0) { // ??java? @SuppressWarnings("unused") boolean flag = false; // ??????????? String fileNamess = UploadImgCommon.getPicNamePathSuffix(); File file = new File(fileNamess); try { muFile.transferTo(file); if (upConf != null) { YunBean yb = new YunBean(); yb.setBucketName(upConf.getBucketName()); yb.setPassword(upConf.getPassWord()); yb.setUserName(upConf.getUserName()); UpYunUtil.yunUp(fileNamess, UploadImgCommon.prefix, yb, UploadImgCommon.suffix); // ?? //LOGGER.debug(LOGGERINFO1 + upConf.getUrlPath() + UploadImgCommon.prefix + UploadImgCommon.suffix); imgMap.put(OLDIMG, upConf.getUrlPath() + UploadImgCommon.prefix + UploadImgCommon.suffix); // ?? int[] widths = UploadImgCommon.getImgSet(imageSetMapper.queryImageSet()); UploadImgCommon.sortWidth(widths); for (int i = 0; i < widths.length; i++) { // ?? //LOGGER.debug(LOGGERINFO2 + widths[i] + LOGGERINFO3 + upConf.getUrlPath() + UploadImgCommon.prefix + UploadImgCommon.suffix + "!" + widths[i]); imgMap.put(widths[i] + "", upConf.getUrlPath() + UploadImgCommon.prefix + UploadImgCommon.suffix + "!" + widths[i]); } } else { flag = true; } } catch (IllegalStateException e) { //LOGGER.error(LOGGERINFO4, e); flag = true; } catch (IOException e) { //LOGGER.error(LOGGERINFO4, e); flag = true; } catch (Exception e) { //LOGGER.error(LOGGERINFO4, e); flag = true; } return imgMap; } else { return null; } } /** * ?? * * @param muFile * @return Map?oldimgsmall? */ public Map<String, String> uploadForOldAndSmall(MultipartFile muFile, UpyunConf upConf) { Map<String, String> imgMap = new HashMap<String, String>(); if (muFile != null && muFile.getSize() != 0) { // ??java? @SuppressWarnings("unused") boolean flag = false; // ??????????? String fileNamess = UploadImgCommon.getPicNamePathSuffix(); File file = new File(fileNamess); try { muFile.transferTo(file); if (upConf != null) { YunBean yb = new YunBean(); yb.setBucketName(upConf.getBucketName()); yb.setPassword(upConf.getPassWord()); yb.setUserName(upConf.getUserName()); UpYunUtil.yunUp(fileNamess, UploadImgCommon.prefix, yb, UploadImgCommon.suffix); // ?? //LOGGER.debug(LOGGERINFO1 + upConf.getUrlPath() + UploadImgCommon.prefix + UploadImgCommon.suffix); imgMap.put(OLDIMG, upConf.getUrlPath() + UploadImgCommon.prefix + UploadImgCommon.suffix); // ??? //LOGGER.debug("==========56?" + upConf.getUrlPath() + UploadImgCommon.prefix + UploadImgCommon.suffix + "!" + SMALL); imgMap.put("small", upConf.getUrlPath() + UploadImgCommon.prefix + UploadImgCommon.suffix + "!" + SMALL); } else { flag = true; } } catch (IllegalStateException e) { //LOGGER.error(LOGGERINFO4, e); flag = true; } catch (IOException e) { //LOGGER.error(LOGGERINFO4, e); flag = true; } catch (Exception e) { //LOGGER.error(LOGGERINFO4, e); flag = true; } return imgMap; } else { return null; } } /** * * * @param item * @return ? */ public String uploadForRichEdit(FileItem item, UpyunConf upConf) { String resoult = null; if (item != null && item.getSize() != 0) { // ??java? @SuppressWarnings("unused") boolean flag = false; // ??????????? String fileNamess = UploadImgCommon.getPicNamePathSuffix(); File file = new File(fileNamess); try { item.write(file); if (upConf != null) { YunBean yb = new YunBean(); yb.setBucketName(upConf.getBucketName()); yb.setPassword(upConf.getPassWord()); yb.setUserName(upConf.getUserName()); UpYunUtil.yunUp(fileNamess, UploadImgCommon.prefix, yb, UploadImgCommon.suffix); // ?? //LOGGER.debug(LOGGERINFO1 + upConf.getUrlPath() + UploadImgCommon.prefix + UploadImgCommon.suffix); resoult = upConf.getUrlPath() + UploadImgCommon.prefix + UploadImgCommon.suffix; } else { flag = true; } } catch (IllegalStateException e) { //LOGGER.error(LOGGERINFO4, e); flag = true; } catch (IOException e) { //LOGGER.error(LOGGERINFO4, e); flag = true; } catch (Exception e) { //LOGGER.error(LOGGERINFO4, e); flag = true; } return resoult; } else { return null; } } /** * ???? * * @param muFile * @return Map?oldimgsmall? */ public Map<String, String> testUpYun(MultipartFile muFile, UpyunConf upConf) throws IOException { Map<String, String> imgMap = new HashMap<String, String>(); if (muFile != null && muFile.getSize() != 0) { // ??????????? String fileNamess = UploadImgCommon.getPicNamePathSuffix(); File file = new File(fileNamess); muFile.transferTo(file); if (upConf != null) { YunBean yb = new YunBean(); yb.setBucketName(upConf.getBucketName()); yb.setPassword(upConf.getPassWord()); yb.setUserName(upConf.getUserName()); KUpYunUtil.kYunUp(fileNamess, UploadImgCommon.prefix, yb, UploadImgCommon.suffix); // ?? //LOGGER.debug(LOGGERINFO1 + upConf.getUrlPath() + UploadImgCommon.prefix + UploadImgCommon.suffix); imgMap.put(OLDIMG, upConf.getUrlPath() + UploadImgCommon.prefix + UploadImgCommon.suffix); // ?? int[] widths = UploadImgCommon.getImgSet(imageSetMapper.queryImageSet()); UploadImgCommon.sortWidth(widths); for (int i = 0; i < widths.length; i++) { // ?? //LOGGER.debug(LOGGERINFO2 + widths[i] + LOGGERINFO3 + upConf.getUrlPath() + UploadImgCommon.prefix + UploadImgCommon.suffix + "!" + widths[i]); imgMap.put(widths[i] + "", upConf.getUrlPath() + UploadImgCommon.prefix + UploadImgCommon.suffix + "!" + widths[i]); } } return imgMap; } else { return null; } } }