最近做一个活动,需要上传logo并压缩图片到适合的尺寸,但是这次由于给予使用率不高,所以想把图片压缩存到数据库里面,然后再前台通过流将图片转换回来
主要使用了java的
<dependency>
<groupId>im4java</groupId>
<artifactId>im4java</artifactId>
<version>1.1.0</version>
</dependency>
1、前台从controller过来
@RequestMapping(method = RequestMethod.POST,value="/logo")
public String uploadCompanyLogo(ModelMap model,@PathVariable Integer companyId,HttpServletRequest request) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile logo = multipartRequest.getFile("logo");
MultipartFile portrait = multipartRequest.getFile("portrait");
this.putCompanyInfo(model,companyId);
try {
if(logo!=null && !logo.isEmpty()){
byte[] big = imageOperationService.resiizeImage(ImageSizeEnum.LOGOBIGWIDTH.getValue(), ImageSizeEnum.LOGOBIGHEIGHT.getValue(), logo.getInputStream());
byte[] small = imageOperationService.resiizeImage(ImageSizeEnum.LOGOSMALLWIDTH.getValue(), ImageSizeEnum.LOGOSMALLHEIGHT.getValue(), logo.getInputStream());
companyService.saveLogo(big, companyId);
companyService.saveLogoSmall(small, companyId);
}
if(portrait!=null && !portrait.isEmpty()){
byte[] data = imageOperationService.resiizeImage(ImageSizeEnum.PORTRAITHEIGHT.getValue(), ImageSizeEnum.PORTRAITHEIGHT.getValue(), portrait.getInputStream());
companyService.updatePortrait(data, companyId);
}
} catch (IOException e) {
// FIXME Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// FIXME Auto-generated catch block
e.printStackTrace();
}
return "company/manage/logo";
}
2,主要是我们service方法的实现了
@Override
public byte[] resiizeImage(int height, int width,InputStream Stream) throws IOException, InterruptedException {
// FIXME Auto-generated method stub
IMOperation op = new IMOperation();
op.addImage("-");
op.resize(height, width);
op.addImage("-");
Pipe pipeIn = new Pipe(Stream,null);
ByteArrayOutputStream fos = new ByteArrayOutputStream();
Pipe pipeOut = new Pipe(null,fos);
// set up command
ConvertCmd convert = new ConvertCmd();
convert.setInputProvider(pipeIn);
convert.setOutputConsumer(pipeOut);
try {
convert.run(op);
} catch (IM4JavaException e) {
// FIXME Auto-generated catch block
e.printStackTrace();
}
Stream.close();
return fos.toByteArray();
}
这样我们就把这个图片的字节流放到了数据库中
3、再来,我们就来显示这张图片
public class ImageShowUtil {
@Value(value = "#{globalProperties['company.business.big']}")
private String biglogurl;
@Value(value = "#{globalProperties['company.business.small']}")
private String smalllogurl;
@Value(value = "#{globalProperties['company.business.logo']}")
private String logourl;
private static final Logger logger = LoggerFactory.getLogger(ImageShowUtil.class);
/**
* 显示byte流图片
* @param data
* @param res
* @throws IOException
*/
@Cacheable(cacheName = "icoCache")
public void showByteImage(byte data[],HttpServletResponse res,String type) throws IOException {
try {
res.setContentType("image/*"); // 设置返回的文件类型
OutputStream toClient = res.getOutputStream(); // 得到向客户端输出二进制数据的对象
if(data!=null&&data.length>0){
toClient.write(data); // 输出数据
}else{
if("big".equals(type)){
URL url = new URL(biglogurl);
InputStream bis = new BufferedInputStream(url.openStream());
toClient.write(getBytes(bis)); // 输出数据
bis.close();
}else if("small".equals(type)){
URL url = new URL(smalllogurl);
InputStream bis = new BufferedInputStream(url.openStream());
toClient.write(getBytes(bis)); // 输出数据
bis.close();
}else{
URL url = new URL(logourl);
InputStream bis = new BufferedInputStream(url.openStream());
toClient.write(getBytes(bis)); // 输出数据
bis.close();
}
toClient.close();
}
} catch (IOException e) // 错误处理
{
logger.error("---------->error:"+e.getMessage());
// PrintWriter toClient = res.getWriter(); // 得到向客户端输出文本的对象
// res.setContentType("text/html;charset=UTF-8");
// toClient.write("无法打开图片!");
// toClient.close();
}
}
public byte[] getBytes(InputStream is) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = new byte[2048];
int len = 0;
while ((len = is.read(b, 0, 2048)) != -1) {
baos.write(b, 0, len);
}
baos.flush();
byte[] bytes = baos.toByteArray();
return bytes;
}
}
这样我们页面的图片的src只想这个servlet就行了,图片就显示出来了
,这样不好就是,如果图片过多的话,每个图片相当于一个http请求,增加系统的负载