Browse Source

[图片上传优化][修改]:jpg优化

release_toaliyun_real
Stormeye Wu 7 years ago
parent
commit
acf98a28af
4 changed files with 46 additions and 64 deletions
  1. +5
    -22
      mallinkAdmin/src/main/java/com/iformall/controller/sys/UploadController.java
  2. +3
    -19
      mallinkBApi/src/main/java/com/iformall/controller/UploadController.java
  3. +36
    -0
      mallinkService/src/main/java/com/iformall/utils/ImgUtil.java
  4. +2
    -23
      mallinkSysAdmin/src/main/java/com/iformall/controller/sys/UploadController.java

+ 5
- 22
mallinkAdmin/src/main/java/com/iformall/controller/sys/UploadController.java View File

@@ -15,6 +15,7 @@ import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.MallResource; import com.iformall.domain.po.MallResource;
import com.iformall.service.MallResourceService; import com.iformall.service.MallResourceService;
import com.iformall.utils.Constant; import com.iformall.utils.Constant;
import com.iformall.utils.ImgUtil;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -26,9 +27,6 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;


import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*; import java.io.*;
import java.net.URL; import java.net.URL;
import java.util.*; import java.util.*;
@@ -146,24 +144,7 @@ public class UploadController extends BaseController {
return data; 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;
}



/** /**
* 图片上传 * 图片上传
@@ -209,8 +190,10 @@ public class UploadController extends BaseController {
} }
fos.close(); fos.close();
fs.close(); fs.close();
logger.info("local file: " + localImgFileName);


File newFile = thinImage(localImgFileName, localNewImgFileName);
File newFile = ImgUtil.thinImage(localImgFileName, localNewImgFileName);
logger.info("local new file: " + localImgFileName);
// 删除本地缓存 // 删除本地缓存
localFile.delete(); localFile.delete();
ObjectMetadata metadata = new ObjectMetadata(); ObjectMetadata metadata = new ObjectMetadata();


+ 3
- 19
mallinkBApi/src/main/java/com/iformall/controller/UploadController.java View File

@@ -16,6 +16,7 @@ import com.iformall.config.AwsProperty;
import com.iformall.domain.po.MallResource; import com.iformall.domain.po.MallResource;
import com.iformall.service.MallResourceService; import com.iformall.service.MallResourceService;
import com.iformall.utils.Constant; import com.iformall.utils.Constant;
import com.iformall.utils.ImgUtil;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -146,24 +147,7 @@ public class UploadController extends BaseController {
return data; 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;
}



/** /**
* 图片上传 * 图片上传
@@ -208,7 +192,7 @@ public class UploadController extends BaseController {
fos.close(); fos.close();
fs.close(); fs.close();


File newFile = thinImage(localImgFileName, localNewImgFileName);
File newFile = ImgUtil.thinImage(localImgFileName, localNewImgFileName);
// 删除本地缓存 // 删除本地缓存
localFile.delete(); localFile.delete();
ObjectMetadata metadata = new ObjectMetadata(); ObjectMetadata metadata = new ObjectMetadata();


+ 36
- 0
mallinkService/src/main/java/com/iformall/utils/ImgUtil.java View File

@@ -0,0 +1,36 @@
package com.iformall.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class ImgUtil {
private final static Logger logger = LoggerFactory.getLogger(ImgUtil.class);

/**
* 图片瘦身
* @param input
* @param newFile
* @throws Exception
*/
public 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;
}
}

+ 2
- 23
mallinkSysAdmin/src/main/java/com/iformall/controller/sys/UploadController.java View File

@@ -17,6 +17,7 @@ import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.MallResource; import com.iformall.domain.po.MallResource;
import com.iformall.service.MallResourceService; import com.iformall.service.MallResourceService;
import com.iformall.utils.Constant; import com.iformall.utils.Constant;
import com.iformall.utils.ImgUtil;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -28,9 +29,6 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;


import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*; import java.io.*;
import java.net.URL; import java.net.URL;
import java.util.*; import java.util.*;
@@ -147,25 +145,6 @@ public class UploadController extends BaseController {
return data; 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;
}

/** /**
* 图片上传 * 图片上传
* *
@@ -209,7 +188,7 @@ public class UploadController extends BaseController {
fos.close(); fos.close();
fs.close(); fs.close();


File newFile = thinImage(localImgFileName, localNewImgFileName);
File newFile = ImgUtil.thinImage(localImgFileName, localNewImgFileName);
// 删除本地缓存 // 删除本地缓存
localFile.delete(); localFile.delete();
ObjectMetadata metadata = new ObjectMetadata(); ObjectMetadata metadata = new ObjectMetadata();


Loading…
Cancel
Save