文件上传到本地服务器(liunx,windows)都可以 记得给路径哦
效果如图所示
直接上代码public JSONObjectsingleFileUpload(String path, MultipartFile multipartFile) {
JSONObject obj =new JSONObject();
if (multipartFile.isEmpty()) {
return obj.fluentPut("code", "10001").fluentPut("msg", "上传的文件不能为空");
}
String type;
// 图片后缀
String suffixName = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
if ("image/png".equals(multipartFile.getContentType()) ||"image/jpeg".equals(multipartFile.getContentType()) ||"image/jpg".equals(multipartFile.getContentType()) ||"image/fax".equals(multipartFile.getContentType()) ||"image/gif".equals(multipartFile.getContentType())) {
type ="pictures/";
}else {
type ="other/";
}
if (multipartFile.getSize() >1073741824) {
return obj.fluentPut("code", "10001").fluentPut("msg", "文件超过1G了");
}
try {
File file =new File(path + type);
if (!file.exists()) {
file.mkdir();//创建文件夹
}
return obj.fluentPut("code", "10000").fluentPut("msg", "成功了").fluentPut("url", type + DateFormatUtils.format(new Date(), "yyMMddHHmm") + UUID.randomUUID() + suffixName);
}catch (Exception e) {
e.printStackTrace();
return obj.fluentPut("code", "10001").fluentPut("msg", "上传单个文件失败");
}
}
网友评论