上传及预览
@PostMapping("/uploadImg")
public ResponseResult uploadImage(@RequestParam("file") MultipartFile file) {
return imageService.uploadImage(file);
}
public ResponseResult uploadImage(String original, MultipartFile file) {
if (file == null) {
return ResponseResult.FAILED("图片不可以为空.");
}
String contentType = file.getContentType();
log.info("contentType == > " + contentType);
if (TextUtils.isEmpty(contentType)) {
return ResponseResult.FAILED("图片格式错误.");
}
String originalFilename = file.getOriginalFilename();
String type = getType(contentType, originalFilename);
if (type == null) {
return ResponseResult.FAILED("不支持此图片类型");
}
long size = file.getSize();
if (size > maxSize) {
return ResponseResult.FAILED("图片最大仅支持" + (maxSize / 1024 / 1024) + "Mb");
}
long currentMillions = System.currentTimeMillis();
String currentDay = new SimpleDateFormat("yyyy_MM_dd").format(currentMillions);
log.info("current day == > " + currentDay);
String dayPath = imagePath + File.separator + currentDay;
File dayPathFile = new File(dayPath);
if (!dayPathFile.exists()) {
dayPathFile.mkdirs();
}
String targetName = String.valueOf(idWorker.nextId());
String targetPath = dayPath +
File.separator + type + File.separator + targetName + "." + type;
File targetFile = new File(targetPath);
if (!targetFile.getParentFile().exists()) {
targetFile.getParentFile().mkdirs();
}
try {
if (!targetFile.exists()) {
targetFile.createNewFile();
}
log.info("targetFile == > " + targetFile);
file.transferTo(targetFile);
Map<String, String> result = new HashMap<>();
String resultPath = currentMillions + "_" + targetName + "." + type;
result.put("id", resultPath);
result.put("name", originalFilename);
Image image = new Image();
image.setContentType(contentType);
image.setId(targetName);
image.setCreateTime(new Date());
image.setUpdateTime(new Date());
image.setPath(targetFile.getPath());
image.setName(originalFilename);
image.setUrl(resultPath);
image.setState("1");
SobUser sobUser = userService.checkSobUser();
image.setUserId(sobUser.getId());
imageDao.save(image);
return ResponseResult.SUCCESS("文件上传成功").setData(result);
} catch (IOException e) {
e.printStackTrace();
}
return ResponseResult.FAILED("图片上传失败,请稍后重试");
}
@Override
public void viewImage(HttpServletResponse response, String imageId) throws IOException {
String[] paths = imageId.split("_");
String dayValue = paths[0];
String format;
format = new SimpleDateFormat("yyyy_MM_dd").format(Long.parseLong(dayValue));
log.info("viewImage format == > " + format);
String name = paths[1];
String type = name.substring(name.length() - 3);
String targetPath = imagePath + File.separator + format + File.separator +
type +
File.separator + name;
log.info("get image target path === > " + targetPath);
File file = new File(targetPath);
OutputStream writer = null;
FileInputStream fos = null;
try {
response.setContentType("image/png");
writer = response.getOutputStream();
fos = new FileInputStream(file);
byte[] buff = new byte[1024];
int len;
while ((len = fos.read(buff)) != -1) {
writer.write(buff, 0, len);
}
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
fos.close();
}
if (writer != null) {
writer.close();
}
}
}
Java修改图片大小尺寸的简单实现
参照:Java修改图片大小尺寸的简单实现