1.上传图片
图片在客户端JavaScipt的判断,和服务器端action中的判断
A. javaScipt判断
当用户选中要上传的图片后,可为<input type="file" id="imgfile" name= "imgfile" onchange="chkimg(this.value)"> 添加onchange事件
JavaScipt代码如下:
<script type="text/javascript">
var img=null;
function chkimg(inp) //判断文件是否为图片,不只是判断后缀名
{
if(img)img.removeNode(true); //判断其中是否有图片内容,有则清空
if(inp!=null && inp != "")
{
var patn = /\.jpg$|\.jpeg$|\.gif$/i; //图片格式,如果不属于这 几种则不进行图片判断
if(patn.test(inp)) //判断文件名是否是属于上面的图片格式
{
img=document.createElement("img"); //创建一个对象
img.attachEvent("onreadystatechange",isimg); //绑定方法, 图片信息正确时调用
img.attachEvent("onerror",notimg); //绑定方法, 图片信息错误时调用
img.src=inp; //设置其src为得到的路径
}else
{
show1.innerHTML = "不是图片!!"; //给出提示
}
}else{ //当没有值时,则为不上传,不进行判断
show1.innerHTML = "";
}
}
function notimg()
{
show1.innerHTML = "您插入图片错误,或者该图片已经损坏!";
}
function isimg()
{
show.insertAdjacentElement("BeforeEnd",img);
show1.innerHTML = "图片大小:"+ img.fileSize/1024+
"图片宽度:"+ img.offsetWidth+
"图片高度:"+ img.offsetHeight;
}
</script>
B. action中判断
当跳转到action中时进行图片判断,代码如下:
/**
* 判断图片是否正确,当文件后缀名为图片格式时,才进行此判断
* 注:该方法不能判断动画格式的图片,动画格式的图片无法通过
* @param inputStream 输入流
* @return(当图片文件不正确时,返回False,正确时返回true)
*/
private boolean isValidImage(InputStream inputStream) {
if (inputStream!=null) {
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(inputStream);
try {
decoder.decodeAsRaster(); //check the image format
}catch (Exception e) {
//e.printStackTrace();
System.out.println("image problem");
return false;
}
}
return true;
}
上面为判断图片是否正确,当页面判断通过后,跳转到action中
得到界面上传的文件路径request.getParameter(“”);
创建File对象,File file = new File(“路径”);
得到其后缀名,判断是否为图片格式,如果是则进行图片是否正确判断
如果想要得到图片的宽,高,可用下面方法:
InputStream input = new FileInputStream(file); //输入流
BufferedImage bi = ImageIO.read(input); //得到BufferedImage
注:使用BufferedImage进行判断图片,可以通过动画形式的图片
If(bi != null){ //如果不为空,则表示图片正确,
int width = bi.getWidth(); //上传图片的宽
int height = bi.getHeight(); //上传图片的高
}
当一切都判断完了则进行文件上传,
String name = file.getName(); //原文件路劲名
int random = (int)(Math.random()*10000); //随机数
String fileName = System.currentTimeMillis() + random
name.substring(name.indexOf("."));
//通过得到系统时间加随即数字生成新文件名,避免重复
得到新的文件名,以避免重复,创建输入流
InputStream input = new FileInputStream(file); //输入流
创建缓冲区,创建输入流,进行文件上传,代码如下:
byte[] buff = new byte[4096]; //缓冲区
FileOutputStream output = new FileOutputStream("存放路径+新文件名");
int bytecount = 1;
while((bytecount = input.read(buff, 0, 4096)) != -1){
//当input.read()方法,不能读取到字节流的时候,返回-1
output.write(buff, 0, bytecount); //写入字节到文件
}
output.flush();
output.close();
2.其他文件上传
使用Struts中的FormFile控件上传文件
在界面中使用<html:file property="testFile"></html:file>
在ActionForm中定义FormFile对象;
在Action中得到ActionForm中的FormFile对象。
实例:UploadForm uploadForm = (UploadForm)form;
FormFile file = uploadForm.getTestFile();
然后得到其后缀名,判断是否为图片格式,如果为图片格式则进行图片正确 判断,判断完成后进行文件上传
String name = file.getFileName(); //原文件路劲名
int random = (int)(Math.random()*10000); //随机数
String fileName = System.currentTimeMillis() + random
name.substring(name.indexOf("."));
//通过得到系统时间加随即数字生成 新文件名,避免重复
FileOutputStream output = new FileOutputStream("存放路径+新文件名");
output.write(file.getFileData()); //将文件输出到指定路径
output.flush();
output.close();
进行文件输入输出的时候也可以用上面的方式
上传图片时,改变图片大小并存储
图片上传后,有需要改变图片大小可使用下面方法:
/**
* 改变上传图片大小
* @param img BufferedImage 对象(通过ImageIO.read()方法得到)
* @param name 原文件后缀名
* @param filePath 上传图片存放路径
* @return
*/
public static boolean uploadJPGfile(BufferedImage img, String name,String filePath)
{
try
{
// 转为jpg标准格式//
int random = (int)(Math.random()*10000); //随机数
String fileName = System.currentTimeMillis()+random+name; // 通过得到系统时间加随机数生成新文件名,避免重复
if (img != null) {
int new_w = 150; // 新图片宽
int new_h = 100; // 新图片高
BufferedImage tag = new BufferedImage(new_w, new_h,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(img, 0, 0, new_w, new_h, null); // 绘制缩小后的图
FileOutputStream out = new FileOutputStream(filePath+fileName);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
return true;
} else{
return false;
}
} catch (Exception e) {
System.out.println("异常错误!");
return false;
}
}