ファイル画像のアップロードツールクラス
3449 ワード
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;
}
}