* 根据得到的Spring上传的附件自动的加载保存
*
* @param files
* @return List<Attachment>
* @throws IllegalStateException
* @throws IOException
*/
@Transactional(readOnly = false)
public List<Attachment> saveAllUploadFile(List<MultipartFile> files)
throws IllegalStateException, IOException {
if (files == null || files.size() == 0) {
return null;
}
List<Attachment> attachs = Lists.newArrayList();
//预设存储目录
String fileToPath = Global.getFileRootPath();for (MultipartFile f : files) {
logger.info(f.getOriginalFilename() + "|" + f.getContentType()
+ "|" + f.getSize());
if (!f.isEmpty()) {
// 判断目录
String saveRelativePath = new DateTime().toString("yyyyMM");
String savePath = fileToPath + saveRelativePath;
File path = new File(savePath);
// 没有路径就直接创建
if (!(path.exists() && path.isDirectory())) {
path.mkdirs();
}
// 放到规定目录,用的Spring的自带的transferTo没有用FileUtils()
String saveName = System.currentTimeMillis() + "";
f.transferTo(new File(path + "/" + saveName));
// 然后保存到Attachment
String fileSuffix = f.getOriginalFilename().substring(
f.getOriginalFilename().lastIndexOf("."));
Attachment attach = new Attachment();
attach.setSaveName(saveName);attach.setSavePath(savePath + "/");
attach.setFileName(f.getOriginalFilename());
attach.setFileSize(f.getSize());
attach.setFileSuffix(fileSuffix);
attach.setContentType(f.getContentType());
attach.setSaveRelativePath(saveRelativePath + "/");
attach.setFileType("upload");
attachs.add(attach);
dao.save(attach);
}
}
return attachs;
}
@Transactional(readOnly = false)
public Attachment saveUploadFile(MultipartFile file) throws IllegalStateException, IOException {
if (file == null) {
return null;
}
//预设存储目录
String fileToPath = Global.getFileRootPath();Attachment attach = new Attachment();
if (!file.isEmpty()) {
// 判断目录
String saveRelativePath = new DateTime().toString("yyyyMM");
String savePath = fileToPath + saveRelativePath;
File path = new File(savePath);
// 没有路径就直接创建
if (!(path.exists() && path.isDirectory())) {
path.mkdirs();
}
// 放到规定目录,用的Spring的自带的transferTo没有用FileUtils()
String saveName = System.currentTimeMillis() + "";
file.transferTo(new File(path + "\\" + saveName));
// 然后保存到Attachment
String fileSuffix = file.getOriginalFilename().substring(
file.getOriginalFilename().lastIndexOf("."));
attach.setSaveName(saveName);
attach.setSavePath(savePath + "/");
attach.setFileName(file.getOriginalFilename());
attach.setFileSize(file.getSize());
attach.setFileSuffix(fileSuffix);
attach.setContentType(file.getContentType());
attach.setSaveRelativePath(saveRelativePath + "/");
attach.setFileType("upload");
dao.save(attach);
}
return attach;
}
/**
* 导入文件
*
* @param file
* @return
* @throws IllegalStateException
* @throws IOException
*/
@Transactional(readOnly = false)
public Attachment saveImportUploadFile(MultipartFile file) throws IllegalStateException, IOException {
if (file == null) {
return null;
}
//预设存储目录
String fileToPath = Global.getFileImportRootPath();Attachment attach = new Attachment();
if (!file.isEmpty()) {
// 判断目录
String saveRelativePath = new DateTime().toString("yyyyMM");
String savePath = fileToPath + saveRelativePath;
File path = new File(savePath);
// 没有路径就直接创建
if (!(path.exists() && path.isDirectory())) {
path.mkdirs();
}
// 放到规定目录,用的Spring的自带的transferTo没有用FileUtils()
String saveName = System.currentTimeMillis() + "";
file.transferTo(new File(path + "\\" + saveName));
// 然后保存到Attachment
String fileSuffix = file.getOriginalFilename().substring(
file.getOriginalFilename().lastIndexOf("."));
attach.setSaveName(saveName);
attach.setSavePath(savePath + "/");
attach.setFileName(file.getOriginalFilename());
attach.setFileSize(file.getSize());
attach.setFileSuffix(fileSuffix);
attach.setFileType("import");
attach.setContentType(file.getContentType());
attach.setSaveRelativePath(saveRelativePath + "/");
dao.save(attach);
}
return attach;
}