|
@@ -1,7 +1,9 @@
|
|
package com.qs.mp.web.controller.common;
|
|
package com.qs.mp.web.controller.common;
|
|
|
|
|
|
|
|
|
|
|
|
+import com.alibaba.druid.sql.visitor.functions.If;
|
|
import com.qs.mp.common.core.domain.AjaxResult;
|
|
import com.qs.mp.common.core.domain.AjaxResult;
|
|
|
|
+import com.qs.mp.common.enums.PicHandlerTypeEnum;
|
|
import com.qs.mp.common.qcloud.QcloudFileUtils;
|
|
import com.qs.mp.common.qcloud.QcloudFileUtils;
|
|
import com.qs.mp.common.utils.LogUtil;
|
|
import com.qs.mp.common.utils.LogUtil;
|
|
import com.qs.mp.common.utils.StringUtils;
|
|
import com.qs.mp.common.utils.StringUtils;
|
|
@@ -99,12 +101,25 @@ public class FileUploadController extends BaseApiController {
|
|
*/
|
|
*/
|
|
@PostMapping("image/remote/upload/post/{auth}")
|
|
@PostMapping("image/remote/upload/post/{auth}")
|
|
public AjaxResult postImage(@PathVariable("auth") int auth, @RequestParam("file") MultipartFile file) {
|
|
public AjaxResult postImage(@PathVariable("auth") int auth, @RequestParam("file") MultipartFile file) {
|
|
- return uploadImageFile(auth, file);
|
|
|
|
|
|
+ return uploadImageFile(auth, file, PicHandlerTypeEnum.CROP.getValue());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @PostMapping("image/remote/upload/post/commpress/{type}")
|
|
|
|
+ public AjaxResult postCommpressImage(@PathVariable("type") int type, @RequestParam("file") MultipartFile file) {
|
|
|
|
+ return uploadImageFile(0, file, type);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 上传图形方法
|
|
|
|
+ * @param auth 认证
|
|
|
|
+ * @param file 文件信息
|
|
|
|
+ * @param handlerType 处理类型,1 裁剪,2 压缩
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
private AjaxResult uploadImageFile(
|
|
private AjaxResult uploadImageFile(
|
|
@PathVariable("auth") int auth,
|
|
@PathVariable("auth") int auth,
|
|
- @RequestParam("file") MultipartFile file) {
|
|
|
|
|
|
+ @RequestParam("file") MultipartFile file,
|
|
|
|
+ int handlerType) {
|
|
try {
|
|
try {
|
|
if (null == file) {
|
|
if (null == file) {
|
|
return AjaxResult.error("上传文件参数为空.");
|
|
return AjaxResult.error("上传文件参数为空.");
|
|
@@ -122,7 +137,14 @@ public class FileUploadController extends BaseApiController {
|
|
mimeType = "image/jpg";
|
|
mimeType = "image/jpg";
|
|
}
|
|
}
|
|
LogUtil.info(logger," mimeType:" + mimeType + " fileName:" + file.getOriginalFilename());
|
|
LogUtil.info(logger," mimeType:" + mimeType + " fileName:" + file.getOriginalFilename());
|
|
- String name = commpressPicAndUpLoadOSS(file, mimeType, 480, 480, bucketName);
|
|
|
|
|
|
+ String name = "";
|
|
|
|
+ if (PicHandlerTypeEnum.CROP.getValue().equals(handlerType)) {
|
|
|
|
+ // 裁剪并上传图片
|
|
|
|
+ name = cropPicAndUpLoadOSS(file, mimeType, 480, 480, bucketName);
|
|
|
|
+ }else if (PicHandlerTypeEnum.COMPRESSION.getValue().equals(handlerType)) {
|
|
|
|
+ // 压缩并上传图片
|
|
|
|
+ name = commpressPicAndUpLoadOSS(file, mimeType, bucketName);
|
|
|
|
+ }
|
|
if (StringUtils.isBlank(name)) {
|
|
if (StringUtils.isBlank(name)) {
|
|
LogUtil.error(logger, "图片上传失败.");
|
|
LogUtil.error(logger, "图片上传失败.");
|
|
return AjaxResult.error("图片上传失败.");
|
|
return AjaxResult.error("图片上传失败.");
|
|
@@ -143,12 +165,19 @@ public class FileUploadController extends BaseApiController {
|
|
*/
|
|
*/
|
|
@PutMapping("image/remote/upload/{auth}")
|
|
@PutMapping("image/remote/upload/{auth}")
|
|
public AjaxResult uploadImage(@PathVariable("auth") int auth, @RequestParam("file") MultipartFile file) {
|
|
public AjaxResult uploadImage(@PathVariable("auth") int auth, @RequestParam("file") MultipartFile file) {
|
|
- return uploadImageFile(auth, file);
|
|
|
|
|
|
+ return uploadImageFile(auth, file, PicHandlerTypeEnum.CROP.getValue());
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- private String commpressPicAndUpLoadOSS(MultipartFile file,String mimeType,int width,int height,String bucketName) {
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 裁剪图片并上传
|
|
|
|
+ * @param file
|
|
|
|
+ * @param mimeType
|
|
|
|
+ * @param width
|
|
|
|
+ * @param height
|
|
|
|
+ * @param bucketName
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ private String cropPicAndUpLoadOSS(MultipartFile file,String mimeType,int width,int height,String bucketName) {
|
|
String fileName = file.getOriginalFilename();
|
|
String fileName = file.getOriginalFilename();
|
|
int idx = fileName.lastIndexOf('.');
|
|
int idx = fileName.lastIndexOf('.');
|
|
String suffix = "";
|
|
String suffix = "";
|
|
@@ -191,4 +220,66 @@ public class FileUploadController extends BaseApiController {
|
|
}
|
|
}
|
|
return name;
|
|
return name;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 压缩并上传图片
|
|
|
|
+ * @param file
|
|
|
|
+ * @param mimeType
|
|
|
|
+ * @param bucketName
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ private String commpressPicAndUpLoadOSS(MultipartFile file, String mimeType, String bucketName){
|
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
|
+ int idx = fileName.lastIndexOf('.');
|
|
|
|
+ String suffix = "";
|
|
|
|
+ if (idx > 0) {
|
|
|
|
+ suffix = fileName.substring(idx);
|
|
|
|
+ }
|
|
|
|
+ String name = UUIDUtils.newId() + suffix;
|
|
|
|
+ try {
|
|
|
|
+ QcloudFileUtils.putStream(file.getInputStream(), name, bucketName, mimeType );
|
|
|
|
+ BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
|
|
|
|
+ Builder<BufferedImage> builder = Thumbnails.of(bufferedImage);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ int pWidth = bufferedImage.getWidth();
|
|
|
|
+ int pHeight = bufferedImage.getHeight();
|
|
|
|
+ builder.size(pWidth, pHeight);
|
|
|
|
+
|
|
|
|
+ String outFileDir = filePath+"/thumb";
|
|
|
|
+ File tempFile = new File(outFileDir);
|
|
|
|
+ if(!tempFile.exists()) {
|
|
|
|
+ tempFile.mkdirs();
|
|
|
|
+ }
|
|
|
|
+ String thumbName = name +"_s";
|
|
|
|
+ String outFilePath = filePath+"/thumb/"+thumbName+"."+mimeType.substring(mimeType.lastIndexOf("/")+1);
|
|
|
|
+ builder.toFile(outFilePath);
|
|
|
|
+
|
|
|
|
+ // 压缩图片
|
|
|
|
+ Thumbnails.of(outFilePath).scale(0.5f).outputQuality(0.5f).toFile(outFilePath);
|
|
|
|
+
|
|
|
|
+ // 压缩图片
|
|
|
|
+ if (pWidth > 500) {
|
|
|
|
+
|
|
|
|
+ float scale = 500f / pWidth;
|
|
|
|
+
|
|
|
|
+ Thumbnails.of(outFilePath).scale(scale).outputQuality(0.5f).toFile(outFilePath);
|
|
|
|
+ }else {
|
|
|
|
+ Thumbnails.of(outFilePath).scale(1f).outputQuality(0.5f).toFile(outFilePath);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ File outFile = new File(outFilePath);
|
|
|
|
+ QcloudFileUtils.putFile(outFile, thumbName, bucketName, mimeType);
|
|
|
|
+ if(outFile.exists()) {
|
|
|
|
+ LogUtil.info(log, "delete file..."+outFilePath);
|
|
|
|
+ outFile.delete();
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ LogUtil.error(log, e, "");
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ return name;
|
|
|
|
+ }
|
|
}
|
|
}
|