package cn.js.fan.util;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import magick.ImageInfo;
import magick.MagickException;
import magick.MagickImage;
public class JMagickServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public JMagickServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取图片URL
String imageURL = request.getParameter("url");
//获取服务器保存缩略照片文件夹的路径
String realURL = this.getServletConfig().getServletContext().getRealPath("/")+"upfile\\wml\\";
//截取图片的相对路径
String tempURL = imageURL.substring(imageURL.indexOf("upfile"));
System.out.println(tempURL.substring(7));
//组合成文件在服务器的绝对路径
String str = (tempURL.substring(7)).replaceAll("/", "\\\\");
//获取原图片的绝对路径
String str1 = imageURL.substring(imageURL.indexOf("upfile")).replaceAll("/", "\\\\");
String n_url = this.getServletConfig().getServletContext().getRealPath("/")+str1;
String wapURL = realURL+str;
if((new File(wapURL).exists())) {//判断该图片是否已经缩小
System.out.println("该图片已缩小");
this.imageStream(wapURL, request, response);//输出图像的文件流
return ;
}
//获取图片的绝对路径
String normalPicURL = this.getServletConfig().getServletContext().getRealPath("/")+"upfile\\"+str;
FileInputStream in = new FileInputStream(normalPicURL);
BufferedImage bi = ImageIO.read(in);
int x = bi.getWidth();//获取图片的宽
int y = bi.getHeight();//获取图片的高
int narrow_x = 0;
int narrow_y = 0;
//判断图片是否需要缩小
if(x > 120 || y > 120) {
if(x > y) {
narrow_y = y/(x/120);
narrow_x = 120;
}
if(y > x) {
narrow_x = x/(y/120);
narrow_y = 120;
}
}else{
System.out.println("图片不用缩小");
this.imageStream(normalPicURL, request, response);//输出图像的文件流
return;
}
String [] s = tempURL.split("/");
String path = "";
//判断路径上的文件夹是否存在,不存在则创建
for(int i=1; i<s.length-1; i++) {
File f2 = new File(realURL+s[i]);
if(!f2.exists()) {
f2.mkdir();
}
realURL += s[i]+"\\";
path += s[i]+"/";
}
//图片生成的路径
String URL = realURL+s[s.length-1];
System.out.println(n_url);
//图片进行缩略
try{
System.setProperty("jmagick.systemclassloader","no");
ImageInfo info = new ImageInfo(n_url);
MagickImage image = new MagickImage(info);
MagickImage scaleImg = image.scaleImage(narrow_x, narrow_y);
scaleImg.setFileName(URL);
scaleImg.writeImage(info);
scaleImg.destroyImages();
image.destroyImages();
this.imageStream(URL, request, response);//输出
}catch(Exception e) {
e.printStackTrace();
}
}
public void imageStream(String url,HttpServletRequest request, HttpServletResponse response) {
try {
String [] str = url.split("\\.");
FileInputStream in = new FileInputStream(url);
int i = in.available();
byte [] data = new byte[i];
in.read(data);//读取图像数据流
in.close();
response.setContentType("image/"+str[str.length-1]);
OutputStream out = response.getOutputStream();
out.write(data);//将图像数据写入输出流
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
页面显示
<img src="/servlet/JMagickServlet?url=图片路径"/>
[b]如果在多个web都使用 JMagick 时可能会出现 jmagick.dll already loaded in another classloader 的报错,原因是 JVM 已经载入了, 可以把 jmagick.jar 的包 放到 服务器的lib里面去 解决。 [/b]