如何让列表的产品图片进行多线程压缩(缩略图)并显示。
[b]问题描述:[/b]
1、产品后台管理系统部署在A6服务器上,客户在这对服务器上通过后台管理系统进行对产品的维护。
1.1、产品维护时,上传产品图片时控制在200K内,却没有做缩略图。
2、产品质量查询系统部署在A3服务器上,供互联网用户进行查询产品信息。
2.1、当互联网通过产品质量信息查询系统查询产品列表时,显示产品列表(产品主图,产品名称等信息)时,显示图片很慢,图片显示格式
[b]问题解决方案:[/b]
1、为了弥补产品主图的缩略图问题,我们在显示的时候把http图片进行压缩(做成缩略图)传输,然后显示,这样比之前显示快很多!原有的图片有80K,现在经过压缩显示为30K。
2、图片大小问题解决了,但是图片显示还是慢了点,不理想,后来发现图片显示是一个接着一个显示的,也就是说处理图片压缩的类只能一个一个进行压缩,一个压缩完了才压缩第二个。
3、这时我想到了多线程处理方式,让多线程类创建五个线程同时进行压缩并显示,问题就出现在这里,发现我的多线程不是自己想像的那样进行处理的,但不知道问题出现在哪里?希望各位帮找找原因。
[color=red][b]期望效果:一打开产品列表,图片压缩同时对6张图片进行压缩并显示[/b][/color]
原代码如下:
1、图片压缩类
2、图片多线程类
3、图片Servlet类
4、JSP产品列表页面MyJsp.jsp
[b]问题描述:[/b]
1、产品后台管理系统部署在A6服务器上,客户在这对服务器上通过后台管理系统进行对产品的维护。
1.1、产品维护时,上传产品图片时控制在200K内,却没有做缩略图。
2、产品质量查询系统部署在A3服务器上,供互联网用户进行查询产品信息。
2.1、当互联网通过产品质量信息查询系统查询产品列表时,显示产品列表(产品主图,产品名称等信息)时,显示图片很慢,图片显示格式
<img src="A6服务器IP+图片名称(1.jpg)" width="75" height="90"/>
,所以在显示时只改变了图片的width,height,而没有改变图片本身的质量,所以很慢。
[b]问题解决方案:[/b]
1、为了弥补产品主图的缩略图问题,我们在显示的时候把http图片进行压缩(做成缩略图)传输,然后显示,这样比之前显示快很多!原有的图片有80K,现在经过压缩显示为30K。
2、图片大小问题解决了,但是图片显示还是慢了点,不理想,后来发现图片显示是一个接着一个显示的,也就是说处理图片压缩的类只能一个一个进行压缩,一个压缩完了才压缩第二个。
3、这时我想到了多线程处理方式,让多线程类创建五个线程同时进行压缩并显示,问题就出现在这里,发现我的多线程不是自己想像的那样进行处理的,但不知道问题出现在哪里?希望各位帮找找原因。
[color=red][b]期望效果:一打开产品列表,图片压缩同时对6张图片进行压缩并显示[/b][/color]
原代码如下:
1、图片压缩类
package me.jwinder.test;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* File :<b>ImageCompress.java</b><br/>
* Time :2009-8-13 上午10:03:55 <br />
* Note :remark...<br />
* @author HF-JWinder
* @version 1.0
*/
public class ImagesUtil
{
public ImagesUtil() {}
private static ImagesUtil instance ;
public static ImagesUtil getInstance()
{
if(null == instance)
{
instance = new ImagesUtil();
}
return instance ;
}
/**
* Method:图片缩小,包括图片尺寸缩小和图片像素(质量)减小<br>
* Remark:一般在客户端显示大图片,比如产品列表的产品主图片<br>
* Author:HF-JWinder(2009-8-13 下午01:39:54)
* @param in InputStream
* @param out OutputStream
* @param width 宽
* @param height 高
*/
private void zoomout(InputStream in, OutputStream out, int width, int height)
{
try {
JPEGImageDecoder decoderFile = JPEGCodec.createJPEGDecoder(in);
BufferedImage imageFile = decoderFile.decodeAsBufferedImage();
// 获得目标图片的宽高,同时乘以放缩比例得到新图片大小
int w = width;
int h = height;
// 建立一个新图片的缓存图片
BufferedImage bufImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
// 从目标图片上获得Graphics以便画在新图片上,最后一个参数是内部无名类,可以用null代替
Graphics g = bufImage.getGraphics();
g.drawImage(imageFile, 0, 0, w, h, null
/*
new ImageObserver() {
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
{
return true;
}
} */
);
// 编码输出
JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(out);
jpeg.encode(bufImage);
out.flush();
}
catch (ImageFormatException e)
{
e.printStackTrace();
// TODO: handle exception
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 网络缩略图[大小和质量],URL图片进行缩略,服务器进行处理然后显示。
* 图片服务器和显示图片服务器和客户端之间交互显示,对原有图片进行缩放,包括大小和质量。
* @param in 读入图片流
* @param out 写出流
* @throws ImageFormatException
* @throws IOException
*/
public void netZoomout(OutputStream out, String imgURL, int width, int height) throws ImageFormatException, IOException
{
int HttpResult; // 服务器返回的状态
URL url = new URL(imgURL); // 创建URL
URLConnection urlconn = url.openConnection(); // 试图连接并取得返回状态码
urlconn.connect();
HttpURLConnection httpconn =(HttpURLConnection) urlconn;
//通过HTTP连接图片地址
HttpResult = httpconn.getResponseCode();
if(HttpResult != HttpURLConnection.HTTP_OK) // 不等于HTTP_OK说明连接不成功
{
return ;
}
InputStream in = urlconn.getInputStream();
this.zoomout(in, out, width, height);
}
/**
* Method:服务器本地图片压缩<br>
* Remark:...<br>
* Author:HF-JWinder(2009-8-13 下午02:45:13)
* @param out
* @param imgFile
* @param width
* @param height
* @throws FileNotFoundException
*/
public void nativeZoomout(OutputStream out , String imgFile, int width, int height) throws FileNotFoundException
{
File file = new File(imgFile);
InputStream in = new FileInputStream(file);
this.zoomout(in, out, width, height);
}
}
2、图片多线程类
package me.jwinder.test;
import java.io.IOException;
import java.io.OutputStream;
import com.sun.image.codec.jpeg.ImageFormatException;
/**
* File :<b>ImagesUtilThread.java</b><br/>
* Time :2009-8-13 下午03:17:51 <br />
* Note :remark...<br />
* @author HF-JWinder
* @version 1.0
*/
public class ImagesUtilThread extends Thread {
private String url ;
private OutputStream out ;
public ImagesUtilThread(String url, OutputStream out)
{
System.out.println("Thread:" + url);
this.url = url;
this.out = out;
}
public void run()
{
try {
new ImagesUtil().netZoomout(out, url, 75, 90);
} catch (ImageFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3、图片Servlet类
package me.jwinder.test;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* File :<b>ImagesServlet.java</b><br/>
* Time :2009-8-13 上午10:32:11 <br />
* Note :remark...<br />
* @author HF-JWinder
* @version 1.0
*/
public class ImagesServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
String url = request.getParameter("url");
System.out.println(url);
//new ImagesUtil().netZoomout(response.getOutputStream(), url, 75, 90);
new ImagesUtilThread(url, response.getOutputStream()).start();
}
}
4、JSP产品列表页面MyJsp.jsp
<body>
1.
<img src="servlet/ImagesServlet?url=http://211.100.42.6/fireWeb/upload/2006/10/31/20090413072339667107.jpg" border="1"/>
2.
<img src="servlet/ImagesServlet?url=http://211.100.42.6/fireWeb/upload/2005/11/28/200904130723433004190.jpg" border="1"/>
3.
<img src="servlet/ImagesServlet?url=http://211.100.42.6/fireWeb/upload/2006/06/05/20090413072347785922.jpg" border="1"/>
4.
<img src="servlet/ImagesServlet?url=http://211.100.42.6/fireWeb/upload/2001/10/31/200904130723589419479.jpg" border="1"/>
5.
<img src="servlet/ImagesServlet?url=http://211.100.42.6/fireWeb/upload/2001/02/21/200904130724043328489.jpg" border="1"/>
6.
<img src="servlet/ImagesServlet?url=http://211.100.42.6/fireWeb/upload/1997/08/28/200904130723535664934.jpg" border="1"/><br/>
</body>
</html>