public void uploadPicture(
@RequestParam(value = "file", required = false) MultipartFile file,
HttpServletRequest request, HttpServletResponse response) {
ResultDataModel result = new ResultDataModel();
// 獲取圖片原始文件名
String originalFileName = file.getOriginalFilename();
//後綴
String suffix = originalFileName.substring(originalFileName.lastIndexOf("."));
//String name = "" + System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String fileName = sdf.format(new Date())
+ suffix;
String smallFileName = sdf.format(new Date())
+ "small"
+ suffix;
String path = "D:/upload/img";
String filePathName = path + "/" + fileName;
String thumbnailFilePathName = path + "/" + smallFileName;
System.out.println(filePathName);
File dest = new File(filePathName);
// 上傳圖片
try {
// 將上傳的文件寫到服務器上指定的文件。
file.transferTo(dest);
double scale = 0.25d;
Thumbnails
.of(filePathName)
.scale(1f)
.outputQuality(scale)
.outputFormat(
originalFileName.substring(originalFileName
.lastIndexOf(".") + 1))
.toFile(thumbnailFilePathName);
dest.delete();
JSONObject obj = new JSONObject();
obj.put("name", smallFileName);
obj.put("url", thumbnailFilePathName);
result = ResultDataModel.returnSuccess(JSON.toJSONString(obj));
} catch (Exception e) {
e.printStackTrace();
result.setStatus(false);
result.setMessage("系統異常,圖片上傳失敗");
}
toClient(result);
}