public static String saveImg(MultipartFile imgFile, String filePath) {
try {
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
FileInputStream fileInputStream = (FileInputStream) imgFile.getInputStream();
String fileName = KeyUtil.getUUIDKey() + “.png”;
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(filePath + File.separator + fileName));
byte[] bs = new byte[1024];
int len;
while ((len = fileInputStream.read(bs)) != -1) {
bos.write(bs, 0, len);
}
bos.flush();
bos.close();
return fileName;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@PostMapping("/uploadPic")
public RestResult uploadPic(HttpServletRequest request, MultipartFile imgFile) {
try {
String userCode = request.getParameter(“userCode”);
if (StringUtils.isEmpty(userCode)) {
return RestResult.getFailed(“工号不能为空!”);
}
if (null == imgFile) {
return RestResult.getFailed(“上传图片不能为空!”);
}
String filePath = request.getSession().getServletContext().getRealPath(ItemUtil.USER_IMG_PATH);
String fileName = FileUtil.saveImg(imgFile, filePath);
if (StringUtils.isEmpty(fileName)) {
return RestResult.getFailed(“上传图片失败!”);
}
return RestResult.getSuccess(“上传图片成功!”, ItemUtil.USER_IMG_PATH + fileName);
} catch (Exception e) {
e.printStackTrace();
return RestResult.getException();
}
}
保存上传图片的方式,以及手机app上传文件保存方式
最新推荐文章于 2021-07-17 17:03:43 发布