用commons-fileupload上传图片时怎样获取图片的长宽,如300*200这种类型.怎样做?
若用js怎么实现获取图片的长宽?
请哪位仁兄快快帮我解答一下.
[code="java"]
/*
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import org.apache.log4j.Logger;
import org.apache.struts.upload.FormFile;
/**
@author 张欣
*/
public class UploadFile {
//获得日志服务
static Logger logger = Logger.getLogger( UploadFile.class.getName (); ); ;
public static String upLoad(FormFile file,String uploadDir,String newName,int bigersize); throws FileNotFoundException, IOException{
String data = null;
String location = null;
String totalname;
String extend;
StringBuffer fileName;
String contentType;
int size;
int i;
int width;
int height;
File dirPath;
InputStream inputStream = null;
OutputStream outputStream;
//得到上传文件的全名,包括扩展名
totalname = file.getFileName();;
//得到扩展名
i = totalname.lastIndexOf(".");;
//如果没有上传图片,则返回空
if(i==-1);{
return location;
}
extend = totalname.substring(i);;
//得到文件大小
size = file.getFileSize();;
logger.info("#上传图片扩展名为:# =" + extend);;
logger.info("#上传图片大小为:# =" + size/1024 + "K");;
if(!extend.equalsIgnoreCase(".jpg");&&!extend.equalsIgnoreCase(".gif");&&!extend.equalsIgnoreCase(".jpeg");&&!extend.equalsIgnoreCase(".bmp");&&
!extend.equalsIgnoreCase(".png");&&!extend.equalsIgnoreCase(".doc");&&!extend.equalsIgnoreCase(".txt");&&!extend.equalsIgnoreCase(".rar");&&!extend.equalsIgnoreCase(".zip");&&!extend.equalsIgnoreCase(".pdf"););{
return "filetypeWrong";
}
if(size>1024*bigersize);{
return "filesizeWrong";
}
//重命名该文件
fileName = new StringBuffer(newName + extend);;
fileName.toString();;
//判断宽高
inputStream = file.getInputStream();;
BufferedImage bi = ImageIO.read(inputStream);;
// another interface such as:read(new File(filename););;
System.out.println("Width=" + bi.getWidth(););;
System.out.println("Height=" + bi.getHeight(););;
width = bi.getWidth();;
height = bi.getHeight();;
if(width>1500 || height>1500);{
return "filetoohigh";
}
//在图片上写入网站标示的文字
//利用Graphics2D的新特性.
Graphics2D g2D = bi.createGraphics();;
//定义字体(楷体或宋体等); 及其大小
Font myfont=new Font("\u5b8b\u4f53",Font.PLAIN,20);;
g2D.setFont(myfont);;
//字体颜色为黑
g2D.setColor(Color.BLACK);;
//需要写在图片上文字 注意:中文需要操作系统支持中文 如中文window,
//linux下要安装中文字库,并使jdk1.4支持这个中文
String strs="hello everyone 中 文";
//在离顶部 和左边各为20处 写入文字,该处使用了对中文处理的getBytes功能.
g2D.drawString(new String(strs.getBytes("ISO8859_1"););,20,20);;
//必须使用,表示完成
g2D.dispose();;
try {
//使用ImageIO写入图片
ImageIO.write(bi, ext, ThF);;
//原来是使用com.sun.image.codec.jpeg.*的功能,如下.需要下载com.sun.image.codec.jpeg
//FileOutputStream out = new FileOutputStream(ThF);;
//JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);;
//encoder.encode(Bi);;
//out.close();;
}catch (Exception ex); {
throw new Exception(" ImageIo.write error in CreatThum.: "+ex.getMessage(););;
}
//上传
dirPath = new File(uploadDir);;
if (!dirPath.exists();); {
dirPath.mkdirs();;
}
outputStream = new FileOutputStream(uploadDir + fileName);;
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = inputStream.read(buffer, 0, 8192);); != -1); {
outputStream.write(buffer, 0, bytesRead);;
}
location = dirPath.getAbsolutePath();
+ System.getProperty("file.separator"); + fileName;
outputStream.close();;
inputStream.close();;
file.destroy();;
return location;
}
}
[/code]
js代码
var image=document.getElementById("imge");
alert(image.width+":"+image.height);