Commit 4be19ace by 墨竹

fix:上传文件名字修改

parent e1878dfb
......@@ -23,19 +23,20 @@ import java.util.HashSet;
import java.util.Set;
@RestController
public class UploadController extends WebBaseController{
private static Logger logger= LoggerFactory.getLogger(UploadController.class);
private static Set<String> FILE_TYPE = new HashSet<>( );
public class UploadController extends WebBaseController {
private static Logger logger = LoggerFactory.getLogger(UploadController.class);
private static Set<String> FILE_TYPE = new HashSet<>();
static {
for(BucketNameEnum value: BucketNameEnum.values()){
FILE_TYPE.add( value.getName() );
for (BucketNameEnum value : BucketNameEnum.values()) {
FILE_TYPE.add(value.getName());
}
}
@RequestMapping("/upload-file")
@ResponseBody
public HaobanResponse upload(@RequestParam MultipartFile file,@RequestParam String fileType) throws Exception{
if(file == null || file.isEmpty()){
public HaobanResponse upload(@RequestParam MultipartFile file, @RequestParam String fileType) throws Exception {
if (file == null || file.isEmpty()) {
return resultResponse(HaoBanErrCode.ERR_2);
}
String name = file.getOriginalFilename();
......@@ -47,24 +48,22 @@ public class UploadController extends WebBaseController{
}
String suffix = name.substring(name.lastIndexOf(".") + 1);
name = SecureUtil.md5(name) + "." + suffix;
String location = System.getProperty( "user.dir" )+"/data/tmp/";
String location = System.getProperty("user.dir") + "/data/tmp/";
//b
long len = file.getSize();
File templateFile = new File( location+name );
FileUtils.copyInputStreamToFile( file.getInputStream(),templateFile );
File templateFile = new File(location + name);
FileUtils.copyInputStreamToFile(file.getInputStream(), templateFile);
// 2.上传腾讯云
String dayFilePath = LocalDateTime.now().format( DateTimeFormatter.ofPattern( DateUtil.FORMAT_DATETIME_14 ) );
String dayFilePath = LocalDateTime.now().format(DateTimeFormatter.ofPattern(DateUtil.FORMAT_DATETIME_14));
String key = "haoban/"+dayFilePath+"/"+name;
fileType = FILE_TYPE.contains( fileType )?fileType:BucketNameEnum.OTHER_90000.getName();
String key = "haoban/" + dayFilePath + "/" + mediaName;
fileType = FILE_TYPE.contains(fileType) ? fileType : BucketNameEnum.OTHER_90000.getName();
try {
String url = FileUploadUtil.simpleUploadFileFromLocal(templateFile, key, fileType);
if (templateFile != null) {
templateFile.delete();
}
JSONObject res = new JSONObject();
res.put("name", name);
res.put("mediaName", mediaName);
......@@ -73,9 +72,8 @@ public class UploadController extends WebBaseController{
res.put("key", key);
res.put("url", "https://" + url);
return resultResponse(HaoBanErrCode.ERR_1, res);
}catch (Exception e) {
e.printStackTrace();
logger.info("异常:{}",e);
} catch (Exception e) {
logger.info("异常:{}", e.getMessage(), e);
return resultResponse(HaoBanErrCode.ERR_0);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment