Java代码
<BODY>
<script language="javascript">
function ShowImage(path){
document.all.divShow.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = path
}
function test(){
document.all.showimg.src=document.all.file1.value
}
</script>
<INPUT style="Z-INDEX: 101LEFT: 232pxPOSITION: absoluteTOP: 272px" type="file"onchange="ShowImage(this.value)">
<div id="divShow" style="FILTER:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image)WIDTH:274pxHEIGHT:100px">
<input type="file" id="file1" onchange="test()"><br/>
<img id="showimg" style="width:200pxheight:200px">
</BODY>
把图片按照规定的比例压缩,然后保存至FTP,列表读取缩略图,单击显示原图。
/*** 压缩图片方法一(高质量)
* @param oldFile 将要压缩的图片
* @param width 压缩宽
* @param height 压缩高
* @param smallIcon 压缩图片后,添加的扩展名(在图片后缀名前添加)
* @param quality 压缩质量 范围:<i>0.0-1.0</i> 高质量:<i>0.75</i> 中等质量:<i>0.5</i> 低质量:<i>0.25</i>
* @param percentage 是否等比压缩 若true宽高比率将将自动调整
*/
public static void compressImage(String oldFile, int width, int height, String smallIcon,
float quality, boolean percentage) {
try {
File file = new File(oldFile)
// 验证文件是否存在
if(!file.exists())
throw new FileNotFoundException("找不到原图片!")
// 获取图片信息
BufferedImage image = ImageIO.read(file)
int orginalWidth = image.getWidth()
int orginalHeight = image.getHeight()
// 验证压缩图片信息
if (width <= 0 || height <= 0 || !Pattern.matches("^[1-9]\\d*$", String.valueOf(width))
|| !Pattern.matches("^[1-9]\\d*$", String.valueOf(height)))
throw new Exception("图片压缩后的高宽有误!")
// 等比压缩
if (percentage) {
double rate1 = ((double) orginalWidth) / (double) width + 0.1
double rate2 = ((double) orginalHeight) / (double) height + 0.1
double rate = rate1 > rate2 ? rate1 : rate2
width = (int) (((double) orginalWidth) / rate)
height = (int) (((double) orginalHeight) / rate)
}
// 压缩后的文件名
String filePrex = oldFile.substring(0, oldFile.lastIndexOf('.'))
String newImage = filePrex + smallIcon + oldFile.substring(filePrex.length())
// 压缩文件存放位置
File savedFile = new File(newImage)
// 创建一个新的文件
savedFile.createNewFile()
// 创建原图像的缩放版本
Image image2 = image.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING)
// 创建数据缓冲区图像
BufferedImage bufImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
// 创建一个Graphics2D
Graphics2D g2 = bufImage.createGraphics()
// 重绘图像
g2.drawImage(image2, 0, 0, width, height, null)
g2.dispose()
// 过滤像素矩阵
float[] kernelData = {
-0.125f, -0.125f, -0.125f,
-0.125f, 2, -0.125f, -0.125f,
-0.125f, -0.125f }
Kernel kernel = new Kernel(3, 3, kernelData)
// 按核数学源图像边缘的像素复制为目标中相应的像素输出像素
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null)
// 转换像素
bufImage = cOp.filter(bufImage, null)
FileOutputStream out = new FileOutputStream(savedFile)
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out)
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufImage)
// 设置压缩质量
param.setQuality(quality, true)
encoder.encode(bufImage, param)
out.close()
System.out.println(newImage)
} catch (Exception e) {
e.printStackTrace()
System.out.println("压缩失败!" + e.getMessage())
}
}
//您好,提问者:
比如您后台查询的是个List集合数组,前台接受。
List<String> list = new ArrayList<String>() //import 你懂的list.add("D:\\xx.jpg")//我不是查询数据库,手动添加图片地址,你也懂的
list.add("D:\\xx1.jpg")
//这个时候如果struts2的话list是public有set get的话肯定前台能拿到
//如果不是就放到request.setAttribute("list",list)中
这下面是页面代码:
<%List list = request.getAttribute("list")
for(int i = 0 i < list.size() i++){%>
<img src="<%=list.get(i)%>"/>
<%}%>