Commit 94cc08c1 authored by cuijun's avatar cuijun

文件压缩250图片格式

parent ef557864
...@@ -130,6 +130,24 @@ public class UploadController{ ...@@ -130,6 +130,24 @@ public class UploadController{
return JsonResultUtil.createSuccessResultWithObj(uploadService.uploadFile(file,prefix)); return JsonResultUtil.createSuccessResultWithObj(uploadService.uploadFile(file,prefix));
} }
@RequestMapping(value = "/app/unauth/admin/upload250", method = RequestMethod.POST)
public JSONObject uploads250(
@RequestParam("file") MultipartFile file,
@RequestParam(value = "prefix",defaultValue = "admin")String prefix
)throws Exception {
String contentType = file.getContentType(); //图片文件类型
// String fileName = file.getOriginalFilename(); //图片名字
/* if (!contentType.equals("image/jpeg") && !contentType.equals("image/gif")&&!contentType.equals("image/png")) {
return JsonResultUtil.createFailedResult(2001,"格式不对!!!");
}*/
// if (file.getSize() > MAX_DRIVING_LICENSE_SIZE) {
// return JsonResultUtil.createFailedResult(2002,"大小超过限制!!!");
// }
return JsonResultUtil.createSuccessResultWithObj(uploadService.uploadFile250(file,prefix));
}
@RequestMapping(value = "/app/unauth/shp/upload", method = RequestMethod.POST) @RequestMapping(value = "/app/unauth/shp/upload", method = RequestMethod.POST)
public ObjectRestResponse shpUploads( public ObjectRestResponse shpUploads(
@RequestParam("file") MultipartFile file, @RequestParam("file") MultipartFile file,
......
...@@ -25,6 +25,7 @@ import org.springframework.http.ResponseEntity; ...@@ -25,6 +25,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
...@@ -102,6 +103,41 @@ public class UploadService { ...@@ -102,6 +103,41 @@ public class UploadService {
return realFileRelPath; return realFileRelPath;
} }
public String uploadFile250(MultipartFile file, String prefix) throws Exception {
// 创建本日存放目录
DateTime now = DateTime.now();
String dirPathToday = "/" + prefix + "/" + now.toString(DEFAULT_DATE_TIME_FORMATTER);
String redisNoKey = RedisKey.UPLOAD_FILE_NO_PREFIX + now.toString(DEFAULT_DATE_TIME_FORMATTER);
Long no = redisTemplate.opsForValue().increment(redisNoKey);
if (no.equals(1L)) {
redisTemplate.expire(redisNoKey, 1, TimeUnit.DAYS);
}
String fileName = file.getOriginalFilename();
String realFileRelPath = dirPathToday + "/" + no + fileName.substring(fileName.lastIndexOf("."));
// 文件存放路径
String filePath = baseUploadPath + realFileRelPath;
// 将文件写入指定位置
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(filePath));
// 将上传的图片压缩成250*250的尺寸
BufferedImage originalImage = ImageIO.read(new File(filePath));
BufferedImage resizedImage = new BufferedImage(250, 250, originalImage.getType());
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, 250, 250, null);
g.dispose();
// 保存压缩后的图片
String compressedFilePath = baseUploadPath + dirPathToday + "/compressed_" + no + ".jpg";
ImageIO.write(resizedImage, "jpg", new File(compressedFilePath));
realFileRelPath = xx_url + SystemConfig.XXMP_URL + realFileRelPath;
return realFileRelPath;
}
/** /**
* 写入上传文件,返回相对路径 * 写入上传文件,返回相对路径
* @param file * @param file
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment