Skip to content

Commit

Permalink
上传文件工具类
Browse files Browse the repository at this point in the history
  • Loading branch information
TreasureJade committed Nov 4, 2019
1 parent 40189b5 commit 0bb0c4a
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/main/java/com/swpu/uchain/blog/util/UploadFileUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.swpu.uchain.blog.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
* @author hobo
* @description
*/
@Slf4j
public class UploadFileUtil {

public static String uploadFile(String fileUrl, MultipartFile file) {
File dest = new File(fileUrl);
if (dest.exists()) {
dest.delete();
}
if (!dest.getParentFile().exists()) {
File parentDir = dest.getParentFile();
parentDir.mkdir();
}
try {
file.transferTo(dest);
log.info("文件上传成功,文件路径:{}", fileUrl);
return fileUrl;
} catch (IOException e) {
e.printStackTrace();
log.error("文件上传失败:", e);
return "";
}
}

}

0 comments on commit 0bb0c4a

Please sign in to comment.