public class OrderServiceImp implements OrderService {
@Override
public MyResult upload(MultipartFile file) {
MyResult myResult = new MyResult();
if (file.isEmpty()) {
myResult.setCode(404);
myResult.setMessage("未检测到文件!");
return myResult;
}
//原始名字
String OriginalFilename = file.getOriginalFilename();
//存储名
String filename = System.currentTimeMillis() + "." + OriginalFilename.substring(OriginalFilename.lastIndexOf(".") + 1);
//文件路径
String filepath = "F:\\Git\\";
File dest = new File(filepath + filename);
//判断是否存在该文件夹
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdir();
}
try {
file.transferTo(dest);
} catch (Exception e) {
e.printStackTrace();
myResult.setMessage("上传失败!");
myResult.setCode(500);
}
myResult.setCode(1);
myResult.setMessage("上传成功!");
return myResult;
}
}
后端上传文件
最新推荐文章于 2024-07-22 15:29:27 发布