SpringMVC保存文件工具类--FileUtil

保存multipart文件到本地

package com.clou.douliu.server.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import net.yuanmomo.tools.util.cipher.MD5;
import net.yuanmomo.tools.util.string.StringUtil;
import net.yuanmomo.tools.util.time.DateFormatUtil.Format;
import net.yuanmomo.tools.util.time.DateUtil;

import org.springframework.web.multipart.MultipartFile;

import com.clou.douliu.server.bean.ConfigBean;

public class FileUtil {
	/**
	 * makeParentDirectory: 创建存放文件的父目录. 
* @author * @since JDK 1.6 */ public static String makeParentDirectory(){ //得到配置存放路径 new_icons_path=/home/dooliuManagerIcon/second String directory = ConfigBean.getConfigBean().getNew_icons_path(); String dateDirectory = DateUtil.dateToString(new Date(),Format.YYYYMMDD); File parent = new File(directory + File.separator +dateDirectory); if(!parent.exists()){ // 如果目录不存在则创建目录 parent.mkdirs(); } return parent.getAbsolutePath(); } /** * saveSharePictureList: 文件上传,保存到本地.
* * @author * @param pictureList * @return * @throws Exception * @since JDK 1.6 */ public static List saveJPGList(List pictureList) throws Exception{ List savedFileList = new ArrayList(); // 需要保存的文件 Iterator iter = pictureList.iterator(); // 保存的路径 String directory = ConfigBean.getConfigBean().getNew_icons_path(); String dateDirectory = DateUtil.dateToString(new Date(),Format.YYYYMMDD); File parent = new File(directory + File.separator +dateDirectory); if(!parent.exists()){ // 如果目录不存在则创建目录 parent.mkdirs(); } while (iter.hasNext()) { MultipartFile multipartFile = iter.next(); // 添加到返回的文件列表 savedFileList.add(saveJPGFile(multipartFile,parent.getAbsolutePath())); } return savedFileList; } /** * saveFile: 保存单个文件.
* * @author * @param multipartFile * @param parent * @return * @throws Exception * @since JDK 1.6 */ public static File saveFile(MultipartFile multipartFile, String parent, String extend) throws Exception { if(parent == null){ // 保存文件的父目录 parent = makeParentDirectory(); } if (multipartFile == null) { return null; } String originalFilename = multipartFile.getOriginalFilename(); if(StringUtil.isNotBlank(originalFilename)){ int index = originalFilename.lastIndexOf("."); if(index > 0){ extend = originalFilename.substring(index); } } // 生成一个随机数,保证同一时间 也不会生成相同的文件名 int temp = (int)Math.random() *1000; String fileName = MD5.getMD5(originalFilename + temp + DateUtil.dateToString(new Date(),Format.YYYY_MM_DD_HH_MM_SS_SSS)); String savePath = parent +File.separator + fileName + extend; File localFile = new File(savePath); try { multipartFile.transferTo(localFile); return localFile; } catch (IllegalStateException e) { throw e; } catch (IOException e) { throw e; } } /** * saveFile: 保存文件集合.
* * @author denghuahui * @param multipartFiles * @param parent * @return * @throws Exception * @since JDK 1.6 */ public static List saveFileList(List multipartFiles, String parent, String extend) throws Exception { List files = new ArrayList<>(); if(parent == null){ // 保存文件的父目录 parent = makeParentDirectory(); } if (multipartFiles == null || multipartFiles.size()<=0) { return null; } for(MultipartFile multipartFile : multipartFiles){ String originalFilename = multipartFile.getOriginalFilename(); if(StringUtil.isNotBlank(originalFilename)){ int index = originalFilename.lastIndexOf("."); if(index > 0){ extend = originalFilename.substring(index); } } // 生成一个随机数,保证同一时间 也不会生成相同的文件名 int temp = (int)Math.random() *1000; String fileName = MD5.getMD5(originalFilename + temp + DateUtil.dateToString(new Date(),Format.YYYY_MM_DD_HH_MM_SS_SSS)); String savePath = parent +File.separator + fileName + extend; File localFile = new File(savePath); try { multipartFile.transferTo(localFile); files.add(localFile); } catch (IllegalStateException e) { throw e; } catch (IOException e) { throw e; } } return files; } /** * saveFile: 保存单个文件.
* * @author * @param multipartFile * @param savePath * @return * @throws Exception * @since JDK 1.6 */ public static File saveJPGFile(MultipartFile multipartFile, String parent) throws Exception { return saveFile(multipartFile,parent,".jpg"); } /** * saveFile: 保存单个文件.
* * @author * @param multipartFile * @param savePath * @return * @throws Exception * @since JDK 1.6 */ public static File saveJPGFile(MultipartFile multipartFile) throws Exception { return saveFile(multipartFile,null,".jpg"); } /** * 保存单个amr(系统语音问题文件) * @param multipartFile * @return * @throws Exception */ public static File saveAmrFile(MultipartFile multipartFile) throws Exception { return saveFile(multipartFile,null,".amr"); } /** * saveFile: 保存文件集合.
* @param multipartFile * @param savePath * @return * @throws Exception * @since JDK 1.6 */ public static List saveJPGFileList(List multipartFiles) throws Exception { return saveFileList(multipartFiles,null,".jpg"); } /** * saveFile: 保存音频文件.
* * @author * @param multipartFile * @param savePath * @return * @throws Exception * @since JDK 1.6 */ public static File saveAMRFile(MultipartFile multipartFile, String parent) throws Exception { return saveFile(multipartFile,parent,".amr"); } /** * saveFile: 保存音频文件.
* * @author * @param multipartFile * @param savePath * @return * @throws Exception * @since JDK 1.6 */ public static File saveAMRFile(MultipartFile multipartFile) throws Exception { return saveFile(multipartFile,null,".amr"); } }


你可能感兴趣的:(Java工具类)