| @@ -14,6 +14,7 @@ import com.iformall.config.AwsProperty; | |||
| import com.iformall.controller.base.BaseController; | |||
| import com.iformall.domain.po.MallResource; | |||
| import com.iformall.service.MallResourceService; | |||
| import com.iformall.utils.Constant; | |||
| import io.swagger.annotations.Api; | |||
| import io.swagger.annotations.ApiOperation; | |||
| import org.slf4j.Logger; | |||
| @@ -25,12 +26,13 @@ import org.springframework.web.bind.annotation.RequestParam; | |||
| import org.springframework.web.bind.annotation.RestController; | |||
| import org.springframework.web.multipart.MultipartFile; | |||
| import java.io.BufferedInputStream; | |||
| import java.io.FileOutputStream; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import javax.imageio.ImageIO; | |||
| import java.awt.*; | |||
| import java.awt.image.BufferedImage; | |||
| import java.io.*; | |||
| import java.net.URL; | |||
| import java.util.*; | |||
| import java.util.List; | |||
| @RestController | |||
| @RequestMapping(value = "upload") | |||
| @@ -144,6 +146,89 @@ public class UploadController extends BaseController { | |||
| return data; | |||
| } | |||
| /** | |||
| * 图片瘦身 | |||
| * @param input | |||
| * @param newFile | |||
| * @throws Exception | |||
| */ | |||
| private static File thinImage(String input,String newFile) throws IOException { | |||
| Image src = ImageIO.read(new File(input)); | |||
| int width = src.getWidth(null); // 获取图源宽度 | |||
| int height = src.getHeight(null); // 获取图源高度 | |||
| BufferedImage thumb = new BufferedImage(width / 1, height / 1, | |||
| BufferedImage.TYPE_INT_RGB); | |||
| // 绘制缩小后的图 | |||
| thumb.getGraphics().drawImage(src, 0, 0, width / 1, height / 1, null); | |||
| File file = new File(newFile); // 输出到文件流 | |||
| ImageIO.write(thumb, "jpg", file); | |||
| return file; | |||
| } | |||
| /** | |||
| * 图片上传 | |||
| * | |||
| * @param multiReq | |||
| * @return | |||
| * @throws Exception | |||
| */ | |||
| @PostMapping(value = "/awsImgUpload", consumes = "multipart/*", headers = "content-type=multipart/form-data") | |||
| @ApiOperation("上传图片") | |||
| public ResultData awsImgUpload(@RequestParam("file") MultipartFile multiReq) throws Exception{ | |||
| logger.info("[" + getIpAddr() + "] UploadController::awsImgUpload"); | |||
| String fileName = UUID.randomUUID().toString(); | |||
| String tempFileName = fileName; | |||
| String fileFormat = ""; | |||
| int dot = multiReq.getOriginalFilename().lastIndexOf('.'); | |||
| if (dot >= 0) { | |||
| fileFormat = multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); | |||
| fileName = getTenantId() + "/" + fileName + fileFormat; | |||
| } else { | |||
| fileName = getTenantId() + "/" + fileName; | |||
| } | |||
| System.out.println(fileName); | |||
| FileOutputStream fos = null; | |||
| if(fileFormat.equalsIgnoreCase(".jpg") | |||
| || fileFormat.equalsIgnoreCase(".jpeg") | |||
| || fileFormat.equalsIgnoreCase(".png")) { | |||
| String localImgFileName = Constant.fileDirectory + File.separator + tempFileName + fileFormat; | |||
| tempFileName = UUID.randomUUID().toString(); | |||
| String localNewImgFileName = Constant.fileDirectory + File.separator + tempFileName + fileFormat; | |||
| BufferedInputStream fs = null; | |||
| File localFile = new File(localImgFileName); | |||
| fos = new FileOutputStream(localFile); | |||
| fs = (BufferedInputStream) multiReq.getInputStream(); | |||
| byte[] buffer = new byte[1024]; | |||
| int len = 0; | |||
| while ((len = fs.read(buffer)) != -1) { | |||
| fos.write(buffer, 0, len); | |||
| } | |||
| fos.close(); | |||
| fs.close(); | |||
| File newFile = thinImage(localImgFileName, localNewImgFileName); | |||
| // 删除本地缓存 | |||
| localFile.delete(); | |||
| ObjectMetadata metadata = new ObjectMetadata(); | |||
| metadata.setContentType(multiReq.getContentType()); | |||
| metadata.setContentLength(newFile.length()); | |||
| ResultData data = awsUpload(new FileInputStream(newFile), metadata, fileName, getTenantId()); | |||
| // 删除本地缓存 | |||
| newFile.delete(); | |||
| return data; | |||
| } else { | |||
| ObjectMetadata metadata = new ObjectMetadata(); | |||
| metadata.setContentType(multiReq.getContentType()); | |||
| metadata.setContentLength(multiReq.getSize()); | |||
| ResultData data = awsUpload(multiReq.getInputStream(), metadata, fileName, getTenantId()); | |||
| return data; | |||
| } | |||
| } | |||
| /** | |||
| * 多文件上传 | |||
| * | |||
| @@ -200,14 +285,14 @@ public class UploadController extends BaseController { | |||
| } | |||
| /** | |||
| * C端上传图片文件 | |||
| * 内部接口-C端上传图片文件 | |||
| * | |||
| * @param multiReq | |||
| * @return | |||
| * @throws Exception | |||
| */ | |||
| @PostMapping(value = "/cimgUpload", consumes = "multipart/*", headers = "content-type=multipart/form-data") | |||
| @ApiOperation("C端上传图片文件") | |||
| @ApiOperation("内部接口-C端上传图片文件") | |||
| public ResultData cimgUpload(@RequestParam("file") MultipartFile multiReq) throws Exception{ | |||
| logger.info("[" + getIpAddr() + "] UploadController::cimgUpload"); | |||
| @@ -7,6 +7,7 @@ public class UrlCheck { | |||
| public static boolean checkUrl(String url) { | |||
| return url.contains("awsFileUpload") | |||
| || url.contains("awsImgUpload") | |||
| || url.contains("awsFilesUpload") | |||
| || url.contains("downQrCode") | |||
| || url.contains("cimgUpload") | |||
| @@ -15,6 +15,7 @@ import com.iformall.common.ResultData; | |||
| import com.iformall.config.AwsProperty; | |||
| import com.iformall.domain.po.MallResource; | |||
| import com.iformall.service.MallResourceService; | |||
| import com.iformall.utils.Constant; | |||
| import io.swagger.annotations.Api; | |||
| import io.swagger.annotations.ApiOperation; | |||
| import org.slf4j.Logger; | |||
| @@ -26,9 +27,13 @@ import org.springframework.web.bind.annotation.RequestParam; | |||
| import org.springframework.web.bind.annotation.RestController; | |||
| import org.springframework.web.multipart.MultipartFile; | |||
| import javax.imageio.ImageIO; | |||
| import java.awt.*; | |||
| import java.awt.image.BufferedImage; | |||
| import java.io.*; | |||
| import java.net.URL; | |||
| import java.util.*; | |||
| import java.util.List; | |||
| @RestController | |||
| @RequestMapping(value = "/api/upload") | |||
| @@ -45,7 +50,7 @@ public class UploadController extends BaseController { | |||
| private AmazonS3 s3 = null; | |||
| private ResultData awsUpload(MultipartFile multiReq, ObjectMetadata metadata, String fileName, String tenantId) { | |||
| private ResultData awsUpload(InputStream inputStream, ObjectMetadata metadata, String fileName, String tenantId) { | |||
| ResultData data = new ResultData(); | |||
| try { | |||
| if (s3 == null) { | |||
| @@ -66,7 +71,7 @@ public class UploadController extends BaseController { | |||
| } | |||
| s3.putObject( | |||
| new PutObjectRequest(awsProperty.getBucketName(), fileName, multiReq.getInputStream(), metadata) | |||
| new PutObjectRequest(awsProperty.getBucketName(), fileName, inputStream, metadata) | |||
| .withCannedAcl(CannedAccessControlList.PublicRead)); | |||
| URL url = s3.getUrl(awsProperty.getBucketName(), fileName); | |||
| logger.info(url.toString()); | |||
| @@ -101,7 +106,7 @@ public class UploadController extends BaseController { | |||
| logger.warn("Error Message: " + ace.getMessage()); | |||
| data.code = ResultData.ERROR; | |||
| data.message = "上传失败"; | |||
| } catch (IOException ioe) { | |||
| } catch (Exception ioe) { | |||
| data.code = ResultData.ERROR; | |||
| logger.warn("Caught an IOException: " + ioe.getMessage()); | |||
| data.code = ResultData.ERROR; | |||
| @@ -119,7 +124,7 @@ public class UploadController extends BaseController { | |||
| */ | |||
| @PostMapping(value = "/awsFileUpload") | |||
| @ApiOperation("上传文件") | |||
| public ResultData awsfileUpload(@RequestParam("file") MultipartFile multiReq) { | |||
| public ResultData awsfileUpload(@RequestParam("file") MultipartFile multiReq) throws Exception { | |||
| logger.info("[" + getIpAddr() + "] UploadController::awsfileUpload"); | |||
| ObjectMetadata metadata = new ObjectMetadata(); | |||
| @@ -137,10 +142,91 @@ public class UploadController extends BaseController { | |||
| fileName = getTenantId() + "/" + fileName; | |||
| } | |||
| ResultData data = awsUpload(multiReq, metadata, fileName, getTenantId()); | |||
| ResultData data = awsUpload(multiReq.getInputStream(), metadata, fileName, getTenantId()); | |||
| return data; | |||
| } | |||
| /** | |||
| * 图片瘦身 | |||
| * @param input | |||
| * @param newFile | |||
| * @throws Exception | |||
| */ | |||
| private static File thinImage(String input,String newFile) throws IOException { | |||
| Image src = ImageIO.read(new File(input)); | |||
| int width = src.getWidth(null); // 获取图源宽度 | |||
| int height = src.getHeight(null); // 获取图源高度 | |||
| BufferedImage thumb = new BufferedImage(width / 1, height / 1, | |||
| BufferedImage.TYPE_INT_RGB); | |||
| // 绘制缩小后的图 | |||
| thumb.getGraphics().drawImage(src, 0, 0, width / 1, height / 1, null); | |||
| File file = new File(newFile); // 输出到文件流 | |||
| ImageIO.write(thumb, "jpg", file); | |||
| return file; | |||
| } | |||
| /** | |||
| * 图片上传 | |||
| * | |||
| * @param multiReq | |||
| * @return | |||
| * @throws Exception | |||
| */ | |||
| @PostMapping(value = "/awsImgUpload", consumes = "multipart/*", headers = "content-type=multipart/form-data") | |||
| @ApiOperation("上传图片") | |||
| public ResultData awsImgUpload(@RequestParam("file") MultipartFile multiReq) throws Exception { | |||
| logger.info("[" + getIpAddr() + "] UploadController::awsImgUpload"); | |||
| String fileName = UUID.randomUUID().toString(); | |||
| String tempFileName = fileName; | |||
| String fileFormat = ""; | |||
| int dot = multiReq.getOriginalFilename().lastIndexOf('.'); | |||
| if (dot >= 0) { | |||
| fileFormat = multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); | |||
| fileName = getTenantId() + "/" + fileName + fileFormat; | |||
| } else { | |||
| fileName = getTenantId() + "/" + fileName; | |||
| } | |||
| System.out.println(fileName); | |||
| FileOutputStream fos = null; | |||
| if(fileFormat.equalsIgnoreCase(".jpg") || fileFormat.equalsIgnoreCase(".jpeg")) { | |||
| String localImgFileName = Constant.fileDirectory + File.separator + tempFileName + fileFormat; | |||
| tempFileName = UUID.randomUUID().toString(); | |||
| String localNewImgFileName = Constant.fileDirectory + File.separator + tempFileName + fileFormat; | |||
| BufferedInputStream fs = null; | |||
| File localFile = new File(localImgFileName); | |||
| fos = new FileOutputStream(localFile); | |||
| fs = (BufferedInputStream) multiReq.getInputStream(); | |||
| byte[] buffer = new byte[1024]; | |||
| int len = 0; | |||
| while ((len = fs.read(buffer)) != -1) { | |||
| fos.write(buffer, 0, len); | |||
| } | |||
| fos.close(); | |||
| fs.close(); | |||
| File newFile = thinImage(localImgFileName, localNewImgFileName); | |||
| // 删除本地缓存 | |||
| localFile.delete(); | |||
| ObjectMetadata metadata = new ObjectMetadata(); | |||
| metadata.setContentType(multiReq.getContentType()); | |||
| metadata.setContentLength(newFile.length()); | |||
| ResultData data = awsUpload(new FileInputStream(newFile), metadata, fileName, getTenantId()); | |||
| // 删除本地缓存 | |||
| newFile.delete(); | |||
| return data; | |||
| } else { | |||
| ObjectMetadata metadata = new ObjectMetadata(); | |||
| metadata.setContentType(multiReq.getContentType()); | |||
| metadata.setContentLength(multiReq.getSize()); | |||
| ResultData data = awsUpload(multiReq.getInputStream(), metadata, fileName, getTenantId()); | |||
| return data; | |||
| } | |||
| } | |||
| /** | |||
| * 多文件上传 | |||
| * | |||
| @@ -150,7 +236,7 @@ public class UploadController extends BaseController { | |||
| */ | |||
| @PostMapping(value = "/awsFilesUpload", consumes = "multipart/*", headers = "content-type=multipart/form-data") | |||
| @ApiOperation("多文件上传") | |||
| public ResultData awsFilesUpload(@RequestParam("files") MultipartFile[] files) { | |||
| public ResultData awsFilesUpload(@RequestParam("files") MultipartFile[] files) throws Exception { | |||
| logger.info("[" + getIpAddr() + "] UploadController::awsFilesUpload"); | |||
| if (files.length > 0) { | |||
| @@ -176,7 +262,7 @@ public class UploadController extends BaseController { | |||
| fileName = getTenantId() + "/" + fileName; | |||
| } | |||
| ResultData data1 = awsUpload(multipartFile, metadata, fileName, getTenantId()); | |||
| ResultData data1 = awsUpload(multipartFile.getInputStream(), metadata, fileName, getTenantId()); | |||
| if (data1.code == ResultData.SUCCESS) { | |||
| Map _data = (Map) data1.data; | |||
| map.put("url", (String) _data.get("url")); | |||
| @@ -7,6 +7,7 @@ public class UrlCheck { | |||
| public static boolean checkUrl(String url) { | |||
| return url.contains("awsFileUpload") | |||
| || url.contains("awsImgUpload") | |||
| || url.contains("awsFilesUpload"); | |||
| } | |||
| @@ -16,6 +16,7 @@ import com.iformall.config.AwsProperty; | |||
| import com.iformall.controller.base.BaseController; | |||
| import com.iformall.domain.po.MallResource; | |||
| import com.iformall.service.MallResourceService; | |||
| import com.iformall.utils.Constant; | |||
| import io.swagger.annotations.Api; | |||
| import io.swagger.annotations.ApiOperation; | |||
| import org.slf4j.Logger; | |||
| @@ -27,11 +28,13 @@ import org.springframework.web.bind.annotation.RequestParam; | |||
| import org.springframework.web.bind.annotation.RestController; | |||
| import org.springframework.web.multipart.MultipartFile; | |||
| import java.io.BufferedInputStream; | |||
| import java.io.FileOutputStream; | |||
| import java.io.IOException; | |||
| import javax.imageio.ImageIO; | |||
| import java.awt.*; | |||
| import java.awt.image.BufferedImage; | |||
| import java.io.*; | |||
| import java.net.URL; | |||
| import java.util.*; | |||
| import java.util.List; | |||
| @RestController | |||
| @RequestMapping(value = "upload") | |||
| @@ -48,7 +51,7 @@ public class UploadController extends BaseController { | |||
| private AmazonS3 s3 = null; | |||
| private ResultData awsUpload(MultipartFile multiReq, ObjectMetadata metadata, String fileName, String tenantId) { | |||
| private ResultData awsUpload(InputStream inputStream, ObjectMetadata metadata, String fileName, String tenantId) { | |||
| ResultData data = new ResultData(); | |||
| try { | |||
| if(s3 == null) { | |||
| @@ -69,7 +72,7 @@ public class UploadController extends BaseController { | |||
| } | |||
| s3.putObject( | |||
| new PutObjectRequest(awsProperty.getBucketName(), fileName, multiReq.getInputStream(), metadata) | |||
| new PutObjectRequest(awsProperty.getBucketName(), fileName, inputStream, metadata) | |||
| .withCannedAcl(CannedAccessControlList.PublicRead)); | |||
| URL url = s3.getUrl(awsProperty.getBucketName(), fileName); | |||
| logger.info(url.toString()); | |||
| @@ -104,7 +107,7 @@ public class UploadController extends BaseController { | |||
| logger.warn("Error Message: " + ace.getMessage()); | |||
| data.code = ResultData.ERROR; | |||
| data.message = "上传失败"; | |||
| } catch (IOException ioe) { | |||
| } catch (Exception ioe) { | |||
| data.code = ResultData.ERROR; | |||
| logger.warn("Caught an IOException: " + ioe.getMessage()); | |||
| data.code = ResultData.ERROR; | |||
| @@ -122,7 +125,7 @@ public class UploadController extends BaseController { | |||
| */ | |||
| @PostMapping(value = "/awsFileUpload", consumes = "multipart/*", headers = "content-type=multipart/form-data") | |||
| @ApiOperation("上传文件") | |||
| public ResultData awsfileUpload(@RequestParam("file") MultipartFile multiReq) { | |||
| public ResultData awsfileUpload(@RequestParam("file") MultipartFile multiReq) throws Exception { | |||
| logger.info("[" + getIpAddr() + "] UploadController::awsfileUpload"); | |||
| ObjectMetadata metadata = new ObjectMetadata(); | |||
| @@ -140,10 +143,91 @@ public class UploadController extends BaseController { | |||
| fileName = getTenantId() + "/" + fileName; | |||
| } | |||
| ResultData data = awsUpload(multiReq, metadata, fileName, getTenantId()); | |||
| ResultData data = awsUpload(multiReq.getInputStream(), metadata, fileName, getTenantId()); | |||
| return data; | |||
| } | |||
| /** | |||
| * 图片瘦身 | |||
| * @param input | |||
| * @param newFile | |||
| * @throws Exception | |||
| */ | |||
| private static File thinImage(String input, String newFile) throws IOException { | |||
| Image src = ImageIO.read(new File(input)); | |||
| int width = src.getWidth(null); // 获取图源宽度 | |||
| int height = src.getHeight(null); // 获取图源高度 | |||
| BufferedImage thumb = new BufferedImage(width / 1, height / 1, | |||
| BufferedImage.TYPE_INT_RGB); | |||
| // 绘制缩小后的图 | |||
| thumb.getGraphics().drawImage(src, 0, 0, width / 1, height / 1, null); | |||
| File file = new File(newFile); // 输出到文件流 | |||
| ImageIO.write(thumb, "jpg", file); | |||
| return file; | |||
| } | |||
| /** | |||
| * 图片上传 | |||
| * | |||
| * @param multiReq | |||
| * @return | |||
| * @throws Exception | |||
| */ | |||
| @PostMapping(value = "/awsImgUpload", consumes = "multipart/*", headers = "content-type=multipart/form-data") | |||
| @ApiOperation("上传图片") | |||
| public ResultData awsImgUpload(@RequestParam("file") MultipartFile multiReq) throws Exception{ | |||
| logger.info("[" + getIpAddr() + "] UploadController::awsImgUpload"); | |||
| String fileName = UUID.randomUUID().toString(); | |||
| String tempFileName = fileName; | |||
| String fileFormat = ""; | |||
| int dot = multiReq.getOriginalFilename().lastIndexOf('.'); | |||
| if (dot >= 0) { | |||
| fileFormat = multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); | |||
| fileName = getTenantId() + "/" + fileName + fileFormat; | |||
| } else { | |||
| fileName = getTenantId() + "/" + fileName; | |||
| } | |||
| System.out.println(fileName); | |||
| FileOutputStream fos = null; | |||
| if(fileFormat.equalsIgnoreCase(".jpg") || fileFormat.equalsIgnoreCase(".jpeg")) { | |||
| String localImgFileName = Constant.fileDirectory + File.separator + tempFileName + fileFormat; | |||
| tempFileName = UUID.randomUUID().toString(); | |||
| String localNewImgFileName = Constant.fileDirectory + File.separator + tempFileName + fileFormat; | |||
| BufferedInputStream fs = null; | |||
| File localFile = new File(localImgFileName); | |||
| fos = new FileOutputStream(localFile); | |||
| fs = (BufferedInputStream) multiReq.getInputStream(); | |||
| byte[] buffer = new byte[1024]; | |||
| int len = 0; | |||
| while ((len = fs.read(buffer)) != -1) { | |||
| fos.write(buffer, 0, len); | |||
| } | |||
| fos.close(); | |||
| fs.close(); | |||
| File newFile = thinImage(localImgFileName, localNewImgFileName); | |||
| // 删除本地缓存 | |||
| localFile.delete(); | |||
| ObjectMetadata metadata = new ObjectMetadata(); | |||
| metadata.setContentType(multiReq.getContentType()); | |||
| metadata.setContentLength(newFile.length()); | |||
| ResultData data = awsUpload(new FileInputStream(newFile), metadata, fileName, getTenantId()); | |||
| // 删除本地缓存 | |||
| newFile.delete(); | |||
| return data; | |||
| } else { | |||
| ObjectMetadata metadata = new ObjectMetadata(); | |||
| metadata.setContentType(multiReq.getContentType()); | |||
| metadata.setContentLength(multiReq.getSize()); | |||
| ResultData data = awsUpload(multiReq.getInputStream(), metadata, fileName, getTenantId()); | |||
| return data; | |||
| } | |||
| } | |||
| /** | |||
| * 多文件上传 | |||
| * | |||
| @@ -153,7 +237,7 @@ public class UploadController extends BaseController { | |||
| */ | |||
| @PostMapping(value = "/awsFilesUpload", consumes = "multipart/*", headers = "content-type=multipart/form-data") | |||
| @ApiOperation("多文件上传") | |||
| public ResultData awsFilesUpload(@RequestParam("files") MultipartFile[] files) { | |||
| public ResultData awsFilesUpload(@RequestParam("files") MultipartFile[] files) throws Exception { | |||
| logger.info("[" + getIpAddr() + "] UploadController::awsFilesUpload"); | |||
| if(files.length > 0){ | |||
| @@ -179,7 +263,7 @@ public class UploadController extends BaseController { | |||
| fileName = getTenantId() + "/" + fileName; | |||
| } | |||
| ResultData data1 = awsUpload(multipartFile, metadata, fileName, getTenantId()); | |||
| ResultData data1 = awsUpload(multipartFile.getInputStream(), metadata, fileName, getTenantId()); | |||
| if(data1.code == ResultData.SUCCESS) { | |||
| Map _data = (Map)data1.data; | |||
| map.put("url", (String) _data.get("url")); | |||
| @@ -208,7 +292,7 @@ public class UploadController extends BaseController { | |||
| */ | |||
| @PostMapping(value = "/cimgUpload", consumes = "multipart/*", headers = "content-type=multipart/form-data") | |||
| @ApiOperation("C端上传图片文件") | |||
| public ResultData cimgUpload(@RequestParam("file") MultipartFile multiReq) { | |||
| public ResultData cimgUpload(@RequestParam("file") MultipartFile multiReq) throws Exception { | |||
| logger.info("[" + getIpAddr() + "] UploadController::cimgUpload"); | |||
| ObjectMetadata metadata = new ObjectMetadata(); | |||
| @@ -217,7 +301,7 @@ public class UploadController extends BaseController { | |||
| String fileName = multiReq.getOriginalFilename(); | |||
| fileName = "cimg/" + fileName; | |||
| ResultData data = awsUpload(multiReq, metadata, fileName, "cimg"); | |||
| ResultData data = awsUpload(multiReq.getInputStream(), metadata, fileName, "cimg"); | |||
| return data; | |||
| } | |||