Java tutorial
/** * Copyright (c) 2012-2014 http://www.eryansky.com * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.eryansky.common.web.utils.upload; import com.eryansky.common.utils.StringUtils; import com.eryansky.common.utils.encode.MD5Util; import com.eryansky.common.web.utils.upload.exception.FileNameLengthLimitExceededException; import com.eryansky.common.web.utils.upload.exception.InvalidExtensionException; import com.eryansky.core.security.LogUtils; import org.apache.commons.fileupload.FileUploadBase; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.time.DateFormatUtils; import org.springframework.validation.BindingResult; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Date; import static org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException; /** * * <p>Date: 2014-5-5 ?8:32 * <p>Version: 1.0 */ public class FileUploadUtils { //? 50M public static final long DEFAULT_MAX_SIZE = 52428800; //? private static String defaultBaseDir = "attached"; //?? public static final int DEFAULT_FILE_NAME_LENGTH = 200; public static final String[] IMAGE_EXTENSION = { "bmp", "gif", "jpg", "jpeg", "png" }; public static final String[] FLASH_EXTENSION = { "swf", "flv" }; public static final String[] MEDIA_EXTENSION = { "swf", "flv", "mp3", "wav", "wma", "wmv", "mid", "avi", "mpg", "asf", "rm", "rmvb" }; public static final String[] DEFAULT_ALLOWED_EXTENSION = { // "bmp", "gif", "jpg", "jpeg", "png", //word excel powerpoint "doc", "docx", "xls", "xlsx", "ppt", "pptx", "html", "htm", "txt", // "rar", "zip", "gz", "bz2", //pdf "pdf" }; private static int counter = 0; public static void setDefaultBaseDir(String defaultBaseDir) { FileUploadUtils.defaultBaseDir = defaultBaseDir; } public static String getDefaultBaseDir() { return defaultBaseDir; } /** * ? * * @param request ? * @param file * @param result ? * @return */ public static final String upload(HttpServletRequest request, MultipartFile file, BindingResult result) { return upload(request, file, result, DEFAULT_ALLOWED_EXTENSION); } /** * ? * * @param request ? * @param file * @param result ? * @param allowedExtension ? * @return */ public static final String upload(HttpServletRequest request, MultipartFile file, BindingResult result, String[] allowedExtension) { try { return upload(request, getDefaultBaseDir(), file, allowedExtension, DEFAULT_MAX_SIZE, true); } catch (IOException e) { LogUtils.logError("file upload error", e); result.reject("upload.server.error"); } catch (InvalidExtensionException.InvalidImageExtensionException e) { result.reject("upload.not.allow.image.extension"); } catch (InvalidExtensionException.InvalidFlashExtensionException e) { result.reject("upload.not.allow.flash.extension"); } catch (InvalidExtensionException.InvalidMediaExtensionException e) { result.reject("upload.not.allow.media.extension"); } catch (InvalidExtensionException e) { result.reject("upload.not.allow.extension"); } catch (FileUploadBase.FileSizeLimitExceededException e) { result.reject("upload.exceed.maxSize"); } catch (FileNameLengthLimitExceededException e) { result.reject("upload.filename.exceed.length"); } return null; } /** * * * @param request ? ??? * @param baseDir * @param file * @param allowedExtension ? null ? * @param maxSize ? -1 ?? * @param needDatePathAndRandomName ?????? * @return ??? * @throws InvalidExtensionException MIME?? * @throws FileSizeLimitExceededException ? * @throws FileNameLengthLimitExceededException ?? * @throws IOException */ public static final String upload(HttpServletRequest request, String baseDir, MultipartFile file, String[] allowedExtension, long maxSize, boolean needDatePathAndRandomName) throws InvalidExtensionException, FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException { int fileNamelength = file.getOriginalFilename().length(); if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) { throw new FileNameLengthLimitExceededException(file.getOriginalFilename(), fileNamelength, FileUploadUtils.DEFAULT_FILE_NAME_LENGTH); } assertAllowed(file, allowedExtension, maxSize); String filename = extractFilename(file, baseDir, needDatePathAndRandomName); File desc = getAbsoluteFile(extractUploadDir(request), filename); file.transferTo(desc); return filename; } private static final File getAbsoluteFile(String uploadDir, String filename) throws IOException { uploadDir = FilenameUtils.normalizeNoEndSeparator(uploadDir); File desc = new File(uploadDir + File.separator + filename); if (!desc.getParentFile().exists()) { desc.getParentFile().mkdirs(); } if (!desc.exists()) { desc.createNewFile(); } return desc; } public static final String extractFilename(MultipartFile file, String baseDir, boolean needDatePathAndRandomName) throws UnsupportedEncodingException { String filename = file.getOriginalFilename(); int slashIndex = filename.indexOf("/"); if (slashIndex >= 0) { filename = filename.substring(slashIndex + 1); } if (needDatePathAndRandomName) { filename = baseDir + File.separator + datePath() + File.separator + encodingFilename(filename); } else { filename = baseDir + File.separator + filename; } return filename; } /** * ???,? * 1?'_'? ' ' * 2?hex(md5(filename + now nano time + counter++)) * 3?[2]_?? * * @param filename * @return */ private static final String encodingFilename(String filename) { filename = filename.replace("_", " "); filename = MD5Util.hash(filename + System.nanoTime() + counter++) + "_" + filename; return filename; } /** * ?// 2013/01/03 * * @return */ private static final String datePath() { Date now = new Date(); return DateFormatUtils.format(now, "yyyy/MM/dd"); } /** * ?? * * @param file * @param allowedExtension null ? * @param maxSize ? ?? -1?? * @return * @throws InvalidExtensionException MIME?? * @throws FileSizeLimitExceededException ? */ public static final void assertAllowed(MultipartFile file, String[] allowedExtension, long maxSize) throws InvalidExtensionException, FileSizeLimitExceededException { String filename = file.getOriginalFilename(); String extension = FilenameUtils.getExtension(file.getOriginalFilename()); if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension)) { if (allowedExtension == IMAGE_EXTENSION) { throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension, filename); } else if (allowedExtension == FLASH_EXTENSION) { throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension, filename); } else if (allowedExtension == MEDIA_EXTENSION) { throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension, filename); } else { throw new InvalidExtensionException(allowedExtension, extension, filename); } } long size = file.getSize(); if (maxSize != -1 && size > maxSize) { throw new FileSizeLimitExceededException("not allowed upload upload", size, maxSize); } } /** * MIME??MIME * * @param extension * @param allowedExtension * @return */ public static final boolean isAllowedExtension(String extension, String[] allowedExtension) { for (String str : allowedExtension) { if (str.equalsIgnoreCase(extension)) { return true; } } return false; } /** * ??? * * @param request * @return */ public static final String extractUploadDir(HttpServletRequest request) { return request.getSession().getServletContext().getRealPath("/"); } public static final void delete(HttpServletRequest request, String filename) throws IOException { if (StringUtils.isEmpty(filename)) { return; } File desc = getAbsoluteFile(extractUploadDir(request), filename); if (desc.exists()) { desc.delete(); } } }