| @@ -18,6 +18,7 @@ import com.iformall.utils.Constant; | |||
| import com.iformall.utils.ImgUtil; | |||
| import io.swagger.annotations.Api; | |||
| import io.swagger.annotations.ApiOperation; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| @@ -173,9 +174,8 @@ public class UploadController extends BaseController { | |||
| System.out.println(fileName); | |||
| FileOutputStream fos = null; | |||
| if(fileFormat.equalsIgnoreCase(".jpg") | |||
| || fileFormat.equalsIgnoreCase(".jpeg") | |||
| || fileFormat.equalsIgnoreCase(".png")) { | |||
| String imgFormat = ImgUtil.getImgFormat(fileFormat); | |||
| if(StringUtils.isNotBlank(imgFormat)) { | |||
| String localImgFileName = Constant.fileDirectory + File.separator + tempFileName + fileFormat; | |||
| String tempFileName1 = UUID.randomUUID().toString(); | |||
| String localNewImgFileName = Constant.fileDirectory + File.separator + tempFileName1 + fileFormat; | |||
| @@ -192,7 +192,7 @@ public class UploadController extends BaseController { | |||
| fs.close(); | |||
| logger.info("local file: " + localImgFileName); | |||
| File newFile = ImgUtil.thinImage(localImgFileName, localNewImgFileName); | |||
| File newFile = ImgUtil.thinImage(localImgFileName, localNewImgFileName, imgFormat); | |||
| logger.info("local new file: " + localImgFileName); | |||
| // 删除本地缓存 | |||
| localFile.delete(); | |||
| @@ -19,6 +19,7 @@ import com.iformall.utils.Constant; | |||
| import com.iformall.utils.ImgUtil; | |||
| import io.swagger.annotations.Api; | |||
| import io.swagger.annotations.ApiOperation; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| @@ -176,7 +177,9 @@ public class UploadController extends BaseController { | |||
| System.out.println(fileName); | |||
| FileOutputStream fos = null; | |||
| if(fileFormat.equalsIgnoreCase(".jpg") || fileFormat.equalsIgnoreCase(".jpeg")) { | |||
| String imgFormat = ImgUtil.getImgFormat(fileFormat); | |||
| if(StringUtils.isNotBlank(imgFormat)) { | |||
| String localImgFileName = Constant.fileDirectory + File.separator + tempFileName + fileFormat; | |||
| String tempFileName1 = UUID.randomUUID().toString(); | |||
| String localNewImgFileName = Constant.fileDirectory + File.separator + tempFileName1 + fileFormat; | |||
| @@ -192,7 +195,7 @@ public class UploadController extends BaseController { | |||
| fos.close(); | |||
| fs.close(); | |||
| File newFile = ImgUtil.thinImage(localImgFileName, localNewImgFileName); | |||
| File newFile = ImgUtil.thinImage(localImgFileName, localNewImgFileName, imgFormat); | |||
| // 删除本地缓存 | |||
| localFile.delete(); | |||
| ObjectMetadata metadata = new ObjectMetadata(); | |||
| @@ -19,9 +19,10 @@ public class ImgUtil { | |||
| * 图片瘦身 | |||
| * @param input | |||
| * @param newFile | |||
| * @param imgFormat png, jpg, gif | |||
| * @throws Exception | |||
| */ | |||
| public static File thinImage(String input, String newFile) throws IOException { | |||
| public static File thinImage(String input, String newFile, String imgFormat) throws IOException { | |||
| Image src = ImageIO.read(new File(input)); | |||
| int width = src.getWidth(null); // 获取图源宽度 | |||
| int height = src.getHeight(null); // 获取图源高度 | |||
| @@ -30,7 +31,17 @@ public class ImgUtil { | |||
| // 绘制缩小后的图 | |||
| thumb.getGraphics().drawImage(src, 0, 0, width / 1, height / 1, null); | |||
| File file = new File(newFile); // 输出到文件流 | |||
| ImageIO.write(thumb, "jpg", file); | |||
| ImageIO.write(thumb, imgFormat, file); | |||
| return file; | |||
| } | |||
| public static String getImgFormat(String fileFormat) { | |||
| String imgFormat = ""; | |||
| if(fileFormat.equalsIgnoreCase(".jpg") || fileFormat.equalsIgnoreCase(".jpeg")) { | |||
| imgFormat = "jpg"; | |||
| } else if(fileFormat.equalsIgnoreCase(".png")) { | |||
| imgFormat = "png"; | |||
| } | |||
| return imgFormat; | |||
| } | |||
| } | |||
| @@ -20,6 +20,7 @@ import com.iformall.utils.Constant; | |||
| import com.iformall.utils.ImgUtil; | |||
| import io.swagger.annotations.Api; | |||
| import io.swagger.annotations.ApiOperation; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| @@ -172,7 +173,8 @@ public class UploadController extends BaseController { | |||
| System.out.println(fileName); | |||
| FileOutputStream fos = null; | |||
| if(fileFormat.equalsIgnoreCase(".jpg") || fileFormat.equalsIgnoreCase(".jpeg")) { | |||
| String imgFormat = ImgUtil.getImgFormat(fileFormat); | |||
| if(StringUtils.isNotBlank(imgFormat)) { | |||
| String localImgFileName = Constant.fileDirectory + File.separator + tempFileName + fileFormat; | |||
| tempFileName = UUID.randomUUID().toString(); | |||
| String localNewImgFileName = Constant.fileDirectory + File.separator + tempFileName + fileFormat; | |||
| @@ -188,7 +190,7 @@ public class UploadController extends BaseController { | |||
| fos.close(); | |||
| fs.close(); | |||
| File newFile = ImgUtil.thinImage(localImgFileName, localNewImgFileName); | |||
| File newFile = ImgUtil.thinImage(localImgFileName, localNewImgFileName, imgFormat); | |||
| // 删除本地缓存 | |||
| localFile.delete(); | |||
| ObjectMetadata metadata = new ObjectMetadata(); | |||