package com.common.util;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Component
public class UploadUtil {
private static String uploadPath;
@Value("${upload.path}")
public void setUploadPath(String uploadPath) {
UploadUtil.uploadPath = uploadPath;
}
/**
* 上传文件
*
* @param applicationName
* @param file
* @return
*/
public static Map upload(String applicationName, MultipartFile file) {
Map map = new HashMap();
String fileName = file.getOriginalFilename();
map.put("name", fileName);
String type = fileName.substring(fileName.lastIndexOf("."));
fileName = new Date().getTime() + type;
File dest = new File(uploadPath + applicationName + "/" + fileName);
if (!dest.getParentFile().exists()) { // 判断文件父目录是否存?
dest.getParentFile().mkdirs();
}
map.put("type", type);
map.put("filePath", applicationName + "/" + fileName);
map.put("localFilePath", uploadPath + applicationName + "/" + fileName);
try {
file.transferTo(dest); // 保存文件
} catch (IllegalStateException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
return map;
}
/**
* 复制单个文件
*
* @param oldPath String 原文件路? 如:c:/fqf.txt
* @param newPath String 复制后路? 如:f:/fqf.txt
* @return boolean
*/
@SuppressWarnin
UploadUtil导入工具类
最新推荐文章于 2024-01-06 11:32:38 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)