上传文件图片工具类

package com.toltech.mczhdj.commons.utils;


import com.toltech.boot.common.utils.TimeUtils;
import com.toltech.mczhdj.commons.bean.UploadBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;


/**
 * UploadUtils 图片文件上传工具类
 */
public class UploadUtils implements Serializable{
	private static final long serialVersionUID = 1L;

	private static UploadBean uploadFile(String uploadPath, String fileDir, MultipartFile file) throws IOException {
		if (file == null || file.getSize() < 1)
			return null;
		StringBuilder builder = new StringBuilder();
		builder.append(fileDir);
		String datePath = TimeUtils.format(new Date(), "yyyy/MM/dd/");
		builder.append(datePath);
		String currentName = createNewFileName(file.getOriginalFilename());
		builder.append(currentName);
		File imageFile = new File(uploadPath + builder.toString());
		File dirFile = imageFile.getParentFile();
		if(!dirFile.exists()) {
			dirFile.mkdirs();
		}
		file.transferTo(imageFile);
		UploadBean bean = new UploadBean();
		bean.setFileName(currentName);
		bean.setFilePath(builder.toString());
		bean.setFileType(file.getContentType());
		bean.setFileSize(file.getSize());
		if(System.getProperty("os.name").toLowerCase().indexOf("linux") >= 0) {
			Runtime.getRuntime().exec("chmod 777 " +uploadPath+bean.getFilePath());
		}
		return bean;
	}

	/**
	 * 上传图片文件
	 * 
	 * @param
	 * @param file
	 * @return
	 * @throws IOException
	 */
	public static UploadBean uploadImage(String uploadPath, MultipartFile file) throws IOException {
		String fileDir = "/image/";
		return uploadFile(uploadPath, fileDir, file);
	}

	/**
	 * 上传其它文件
	 *
	 * @param
	 * @param file
	 * @return
	 * @throws IOException
	 */
	public static UploadBean uploadFile(String uploadPath, MultipartFile file) throws IOException {
		String fileDir = "/file/";
		return uploadFile(uploadPath, fileDir, file);
	}
	
	/**
	 * 根据上传的文件名生成新的统一文件名
	 * @param orginalName
	 * @return
	 */
	public static String createNewFileName(String orginalName){
		if(StringUtils.isNotBlank(orginalName)){
			StringBuffer buffer = new StringBuffer();
			buffer.append(System.currentTimeMillis());
			buffer.append(".");
			buffer.append(StringUtils.substringAfterLast(orginalName, "."));
			return buffer.toString();
		}
		return null;
	}
	
}
package com.toltech.mczhdj.commons.bean;

import java.io.Serializable;

/**
 * UploadBean
 * @author qiuyp
 * @version 1.0
 * @since 2017年2月28日下午2:19:13
 */
public class UploadBean implements Serializable {
	private static final long serialVersionUID = 1L;
	private String fileName;
	private String fileType;
	private long fileSize;
	private String filePath;
	
	public UploadBean(){
		
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getFileType() {
		return fileType;
	}

	public void setFileType(String fileType) {
		this.fileType = fileType;
	}

	public long getFileSize() {
		return fileSize;
	}

	public void setFileSize(long fileSize) {
		this.fileSize = fileSize;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
}

 

你可能感兴趣的:(java)