Java tutorial
/* * @(#)UpLoadAction.java 2011-10-20 * ?us?? 2008-2011, Inc. All rights reserved. * s.server. Use is subject to license terms. */ package com.us.action.file; import java.io.File; import java.util.Arrays; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.servlet.http.HttpServletRequest; import lombok.Setter; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.apache.commons.logging.Log; import com.us.action.Action; import com.us.config.AppHelper; import com.us.i118.I118Helper; import com.us.model.KindResult; import com.us.util.DateUtil; import com.us.util.FileHelper; import com.us.util.JSONHelper; import com.us.util.LogHelper; import com.us.util.RandomHelper; /** * ,?Servlet? * ? * * @author <a href="mailto:monlyu.hong@gmail.com">monlyu</a> * @version --0.0.0--, 2011-10-20 * @since JDK6.0 */ public final class KindUpLoad extends Action { Log log = LogHelper.getLog(); private Object gloabelError; @Setter private File imgFile; // private String imgFileContentType; // MIME @Setter private String imgFileFileName; // ?????? private final static String SIZE_LIMIT_EXCEPTION = "the request was rejected because its size ("; @Override public String execute() throws Exception { if (gloabelError != null) { return text(getError(gloabelError.toString())); } if (imgFile == null) { return text(getError(getI118("${upload.nofile}", getRequest()))); } String dirName = getRequest().getParameter("dir"); if (dirName == null) { dirName = "image"; } final String allowed = AppHelper.KIND_CONFIG.get(dirName).toString(); String fileExt = FilenameUtils.getExtension(imgFileFileName); if (!Arrays.<String>asList(allowed.split(",")).contains(fileExt)) { return text(getError(getI118("${upload.invaildFile}", getRequest(), imgFileFileName, allowed))); } Object savePath = AppHelper.KIND_CONFIG.get("savePath"); if (savePath == null) { return text(getError(getI118("${upload.noConfig}", getRequest(), "savePath"))); } // File uploadDir = FileHelper.getFile(AppHelper.APPSET.getFilePath(), savePath.toString()); if (!uploadDir.exists()) uploadDir.mkdirs(); if (uploadDir == null || !uploadDir.isDirectory()) { return text(getError(getI118("${upload.dirNotExist}", getRequest()))); } // ?? if (!uploadDir.canWrite()) { return text(getError(getI118("${upload.dirCantWrite}", getRequest()))); } Object type = AppHelper.KIND_CONFIG.get(dirName); if (type == null) { return text(getError(getI118("${upload.typeNoConfig}", getRequest(), "dirName"))); } // ?URL String saveUrl = AppHelper.APPSET.getWebPath() + savePath.toString(); if (!uploadDir.exists()) uploadDir.mkdirs(); String child = DateUtil.format(DateUtil.now(), "yyyyMM/dd/"); uploadDir = FileHelper.getFile(uploadDir, child); saveUrl += child; if (!uploadDir.exists()) { uploadDir.mkdirs(); } final long initSize = Long.parseLong(AppHelper.KIND_CONFIG.get("maxSize").toString()); if (imgFile.length() > initSize * 1024000) { return text(getError( getI118("${upload.tooMax}", getRequest(), imgFileFileName, imgFile.length(), initSize))); } String newFileName = RandomHelper.uu_id() + "." + fileExt; File uploadedFile = new File(uploadDir, newFileName); final long startTime = System.currentTimeMillis(); FileUtils.copyFile(imgFile, uploadedFile); log.info(String.format("?%s(ms)", (System.currentTimeMillis() - startTime))); return text(JSONHelper.obj2Json(KindResult .success(getI118("${upload.hasUpload}", getRequest(), imgFileFileName), saveUrl + newFileName))); } @Override public void addActionError(String anErrorMessage) { if (anErrorMessage.startsWith(SIZE_LIMIT_EXCEPTION)) { Matcher m = Pattern.compile("\\d+").matcher(anErrorMessage); String s1 = "0"; if (m.find()) s1 = m.group(); String s2 = "0"; if (m.find()) s2 = m.group(); gloabelError = getI118("${upload.gtSysSet}", getRequest(), FileUtils.byteCountToDisplaySize(Long.parseLong(s1)), FileUtils.byteCountToDisplaySize(Long.parseLong(s2))); } else { super.addActionError(anErrorMessage); } } // ?? private String getError(String source) { return JSONHelper.obj2Json(KindResult.error(source)); } /** * ? * * @param source * @param request * @param args * @return */ private String getI118(String source, HttpServletRequest request, Object... args) { return String.format(I118Helper.i118Value(source, request), args); } }