[quote]首先需要通过JS取得当前选择图片的绝对路径[/quote]
[quote]JAVA 后台处理上传图片。更改它的大小[/quote]
function getPath(obj) {
if(obj){
if (window.navigator.userAgent.indexOf("MSIE")>=1) {
obj.select();
return document.selection.createRange().text;
} else if(window.navigator.userAgent.indexOf("Firefox")>=1) {
if(obj.files){
return obj.files.item(0).getAsDataURL();
}
return obj.value;
}
return obj.value;}
}
function path(aaa){
var s = getPath(aaa);
$("#copyPath").val(s)
}
[quote]页面调用[/quote]
<html:file property="uploadFile" styleId="styleid"
style="width:320px;" onchange="path(this)"></html:file>
[quote]JAVA 后台处理上传图片。更改它的大小[/quote]
package com.wangyp.changeImage;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ChangeImageSize {
/**
* /** 缩放图像
*
* @param file
* 源图像文件地址
* @param result
* 缩放后的图像地址
* @param widths
* 图片宽度
* @param heights
* 图片高度
* @return
*/
public static String scale(String file, int widths, int heights) {
String newPath = ChangeImageSize.getPath(file);
try {
BufferedImage src = ImageIO.read(new File(file)); // 读入文件
Image image = src.getScaledInstance(widths, heights, Image.SCALE_DEFAULT);
BufferedImage tag = new BufferedImage(widths, heights, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 绘制缩小后的图
g.dispose();
ImageIO.write(tag, "JPEG", new File(newPath));// 输出到文件流
} catch (IOException e) {
e.printStackTrace();
}
return newPath;
}
/**
* 根据路径生成新路径
*
* @param result
* 根据给定的路径生成一个同文件夹下的路径,区别是文件名称前加Copy
* @return
*/
public static String getPath(String file) {
String name = file.substring(file.lastIndexOf("\\") + 1, file.lastIndexOf("."));
file = file.replaceAll(name, "Copy" + name);
return file;
}
}