<!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.*"%>
<%@ page import= "common.DrawPic.* "%>
<%
String s="1222221<IMG src="http://csdnimg.cn/www/images/csdnindex_piclogo.gif">3634537547";
String regex="src=[/"||'].[^/"]*[/"||']";
String t[];
int i=0;
int width=0;
int height=0;
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(s);
while (m.find()) { //循环取路径开始
int start = m.start();
int end = m.end();
// t[i]=s.substring(start+5, end-1);
String temp = s.substring(start+5, end-1);
// java.net.URL url = new URL("abc.jpg");
java.net.URL url = new URL("http://www.baidu.com/img/logo-yy.gif");
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;
}
out.print(width);
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();
out.print(largepic);
}
} //循环取路径结束
%>
本文介绍了一种使用Java进行图片处理的方法,包括从URL读取图片、获取图片尺寸、调整图片大小并保存为新的文件。该过程涉及到了HTTP连接的使用及图片的缩放算法。


被折叠的 条评论
为什么被折叠?



