java 画图方式二

来源:互联网 发布:网络游戏软件出售 编辑:程序博客网 时间:2024/05/15 14:15

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page language="java" %>
<%@ page import= "javax.sql.DataSource "%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.util.regex.Matcher" %>
<%@ page import="java.util.regex.Pattern" %>
<%@ page language="java" %>
<%@ page import= "java.io.* "%>
<%@ page import= "java.net.* "%>
<%@ page import= "javax.naming.* "%>
<%@ page import= "javax.sql.DataSource "%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*,java.sql.*,com.jspsmart.upload.*,java.util.*,cn.oof.database.*,cn.oof.house.*"%>
<%@ page import="javax.imageio.*" %>
<%@ page import="java.net.URL" %>
<%@ page import="java.net.URLConnection.*"%>
<%@ page import="java.io.IOException.*"%>
<%
  String   s="<IMG src='http://avatar.csdn.net/4/C/8/1_faleshi.jpg' width>66600<IMG  src='http://avatar.csdn.net/4/C/8/1_faleshi.jpg' />11";
  String ss=s; 
  String  regex="src=/"?(.*?)(/"|>|//s+)";
   int width=0;
   int height=0;
  Pattern p = Pattern.compile(regex);
  Matcher m = p.matcher(s);
  while (m.find()) { //循环取路径开始
  String temp =m.group();
  String temp1=temp.substring(5,temp.length()-2);
       java.net.URL url = new URL(temp1);
     Image src = javax.imageio.ImageIO.read(url); 
   HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
   long len = urlConnection.getContentLength();
   BufferedImage bi = null;
   bi = javax.imageio.ImageIO.read(url);
   int[] a = new int[2];
   if(bi!=null){
   a[0]= bi.getWidth();
   a[1] = bi.getHeight(); //获得 高度
   width=a[0];
   height=a[1];
     if(a[0]>500){width=500;}
     if(a[1]>500){height=500;}
   }
   long size=0;
   if(len>0){
   size=len/1024;
   }
   if(size>0)
   {
  String newurl="";
  String largepic="";
     //更改文件名,取得当前上传时间的毫秒数值
     Calendar calendar = Calendar.getInstance();
     String filename = String.valueOf(calendar.getTimeInMillis());
//按日期创建目录
  String path=request.getRealPath("/");
  Calendar c = Calendar.getInstance();
  int year = c.get(Calendar.YEAR);
  int month = c.get(Calendar.MONTH)+1;//计算机的月份从0开始,所以要加上1
  int date = c.get(Calendar.DATE);
  String dstr = "";
  dstr+=year;
  dstr+=(month<10?"0":"")+month;
  String newdir=path+"/img/"+dstr;
  largepic="/img/"+dstr+"/"+filename+".jpg";
  newurl=newdir+"/"+filename+".jpg";
  java.io.File dir= new java.io.File(newdir);
  if(!dir.exists())
  {
  dir.mkdir();
  }
     float tagsize=200;
     int old_w=width;                                      //得到源图宽
     int old_h=height;  
     int new_w=0;
     int new_h=0;                             //得到源图长
     int tempsize;
     float tempdouble;
     if(old_w>old_h){
      tempdouble=old_w/tagsize;
     }else{
      tempdouble=old_h/tagsize;
     }
     new_w=Math.round(old_w/tempdouble);
     new_h=Math.round(old_h/tempdouble);//计算新图长宽
     BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
     tag.getGraphics().drawImage(src,0,0,new_w,new_h,null);        //绘制缩小后的图
     FileOutputStream newimage=new FileOutputStream(newurl);           //输出到文件流
     JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);      
     encoder.encode(tag);                                                //近JPEG编码
      newimage.close();  
   ss=ss.replace(temp1,largepic);
     }
  }   //循环取路径结束
out.print(ss);
%>