org.zodiac.boot.file.ServletLocalFile Maven / Gradle / Ivy
The newest version!
package org.zodiac.boot.file;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.web.multipart.MultipartFile;
import org.zodiac.boot.config.PlatformUploadFileInfo;
import org.zodiac.sdk.toolkit.constants.StringPool;
import org.zodiac.sdk.toolkit.util.DateTimeUtil;
import org.zodiac.sdk.toolkit.util.ExceptionUtil;
import java.io.File;
import java.io.IOException;
import java.util.Date;
/**
* {@link LocalFile}的{@code Servlet}实现。
*
*/
public class ServletLocalFile extends LocalFile {
/**
* 上传文件
*/
@JsonIgnore
private MultipartFile file;
/**
* 上传物理路径
*/
private String uploadPath;
/**
* 上传虚拟路径
*/
private String uploadVirtualPath;
/**
* 文件名
*/
private String fileName;
/**
* 真实文件名
*/
private String originalFileName;
// /**
// * 文件配置
// */
// private static PlatformUploadFileInfo platformFileInfo;
public ServletLocalFile(PlatformUploadFileInfo platformFileInfo, String dir, MultipartFile file) {
super(platformFileInfo, dir);
this.file = file;
this.fileName = file.getName();
this.originalFileName = file.getOriginalFilename();
this.uploadPath = PlatformFileUtil.formatUrl(
File.separator + getPlatformFileInfo().getUploadRealPath() + File.separator + dir + File.separator
+ DateTimeUtil.format(new Date(), DateTimeUtil.FORMAT_8_a) + File.separator + this.originalFileName);
this.uploadVirtualPath = PlatformFileUtil.formatUrl(
getPlatformFileInfo().getUploadCtxPath().replace(getPlatformFileInfo().getContextPath(), StringPool.EMPTY)
+ File.separator + dir + File.separator + DateTimeUtil.format(new Date(), DateTimeUtil.FORMAT_8_a) + File.separator
+ this.originalFileName);
}
public ServletLocalFile(PlatformUploadFileInfo platformFileInfo, String dir, MultipartFile file, String uploadPath, String uploadVirtualPath) {
this(platformFileInfo, dir, file);
if (null != uploadPath) {
this.uploadPath = PlatformFileUtil.formatUrl(uploadPath);
this.uploadVirtualPath = PlatformFileUtil.formatUrl(uploadVirtualPath);
}
}
public MultipartFile getFile() {
return file;
}
public void setFile(MultipartFile file) {
this.file = file;
}
@Override
public String getUploadPath() {
return uploadPath;
}
public void setUploadPath(String uploadPath) {
this.uploadPath = uploadPath;
}
public String getUploadVirtualPath() {
return uploadVirtualPath;
}
public void setUploadVirtualPath(String uploadVirtualPath) {
this.uploadVirtualPath = uploadVirtualPath;
}
@Override
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
@Override
public String getOriginalFileName() {
return originalFileName;
}
public void setOriginalFileName(String originalFileName) {
this.originalFileName = originalFileName;
}
/**
* 图片上传。
*
* @param fileFactory 文件上传工厂类
* @param compress 是否压缩
*/
@Override
public void transfer(FileProxy fileFactory, boolean compress) {
try {
File file = new File(getUploadPath());
if (null != fileFactory) {
String[] path = fileFactory.path(file, getDir());
this.uploadPath = path[0];
this.uploadVirtualPath = path[1];
file = fileFactory.rename(file, path[0]);
}
File pfile = file.getParentFile();
if (!pfile.exists()) {
pfile.mkdirs();
}
this.file.transferTo(file);
if (compress) {
fileFactory.compress(this.uploadPath);
}
} catch (IllegalStateException | IOException e) {
log.error("{}", ExceptionUtil.stackTrace(e));
}
}
// private static PlatformUploadFileInfo getPlatformFileInfo() {
// if (platformFileInfo == null) {
// platformFileInfo = SpringContextHolder.getBean(PlatformUploadFileInfo.class);
// }
// return platformFileInfo;
// }
}