上傳
@RequestMapping("/uploadImg")
public Result uploadImg(HttpServletRequest request, MultipartFile file){
log.info("/uploadImg->上傳圖片->開始" );
JSONObject jsonObject = new JSONObject();
String mid = request.getParameter("mid");
//String rootPath = System.getProperty("catalina.home");
//rootPath = rootPath + "/" + image_path;
String rootPath = image_path;
File f = new File(rootPath);
if(!f.exists()){
f.mkdir();
}
if (StringUtils.isBlank(mid)){
return Result.error("mid is error");
}
String uuidStr = UUID.randomUUID().toString().replace("-","");
String fileName = uuidStr + ".png";
String filePath = rootPath + "/" + fileName;//本地絕對路徑
String filePathTmp = rootPath + "/" + uuidStr + "_tmp.png";
log.info("[圖片接口]上傳的圖片本地絕對路徑為->" + filePath);
f = new File(filePath);
try {
file.transferTo(f);
} catch (IOException e) {
e.printStackTrace();
throw new AbpException("[圖片接口]寫文件到磁盤失敗");
}
if(UtilImg.createThumbnail(filePath,filePathTmp, Float.parseFloat(image_width),Float.parseFloat(image_height))){
UtilImg.delelteAndRenameFile(filePath,filePathTmp,uuidStr);
}
jsonObject.put("path",fileName);
log.info("/uploadImg->上傳圖片->結束->" + jsonObject.toJSONString() );
return Result.ok(jsonObject);
}
訪問
@RequestMapping("/getImg/{filename}")
public void getImg(HttpServletResponse response, @PathVariable("filename") String filename){
log.info("/getImg->訪問圖片->開始" );
if(StringUtils.isBlank(filename)){
throw new AbpException("[圖片接口]參數有誤");
}
//String rootPath = System.getProperty("catalina.home");
//String filePath = rootPath + "/" + image_path + "/" + filename;
String rootPath = image_path;
String filePath = rootPath + "/" + filename;
File imageFile = new File(filePath);
if (imageFile.exists()){
FileInputStream fis = null;
OutputStream os = null;
try {
fis = new FileInputStream(imageFile);
os = response.getOutputStream();
int count = 0;
byte[] buffer = new byte[1024 * 8];
while((count = fis.read(buffer)) != -1){
os.write(buffer,0,count);
os.flush();
}
log.info("[圖片接口]輸出完成");
} catch (Exception e) {
e.printStackTrace();
throw new AbpException("[圖片接口]打開圖片失敗,可能圖片ID錯誤");
}finally {
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
throw new AbpException("[圖片接口]關閉輸入流失敗");
}
}
if(os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
throw new AbpException("[圖片接口]關閉輸出流失敗");
}
}
}
}else{
throw new AbpException("[圖片接口]圖片ID錯誤");
}
}
上傳的接口文檔

訪問的接口文檔
