用struts1实现图片上传和显示。把图片路径和名字保存到数据库中,在以后显示中从数据中读取文件名,求代码
最好能把上传的图片生成缩率图,然后将其路径和名字保存到数据库中,并在页面上显示。thankyou...
最好能把上传的图片生成缩率图,然后将其路径和名字保存到数据库中,并在页面上显示。 thank you
展开
2个回答
展开全部
给你的struts1的上传示例,
数据库操作这块自己改改就有了,详细看参考资料地址
----------------------------------
大图上传完了,调用这个方法,最后一个参数是是否按同比例缩小
/*
* 生成微缩图
* */
public void createSmallImages(String fromImg,String toImg,int width,int height,boolean scale)
{
try{
BufferedImage fromBI = ImageIO.read(new FileInputStream(fromImg));
if(scale==true)//同比例缩小
{
float xy = (float)width/(float)height;
if(((float)fromBI.getWidth()/fromBI.getHeight())>xy)
{
xy =(float)width/(float)fromBI.getWidth();
}else{
xy =(float)height/(float)fromBI.getHeight();
}
if(xy>1){//图片变大则用原图大小
width = fromBI.getWidth();
height = fromBI.getHeight();
}else{
width = (int)(xy*fromBI.getWidth());
height =(int)(xy*fromBI.getHeight());
}
}
BufferedImage toBI = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = toBI.createGraphics();
g2.drawImage(fromBI.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
g2.dispose();
ImageIO.write(toBI, "JPG", new File(toImg));
}catch(Exception e)
{
e.printStackTrace();
}
}
数据库操作这块自己改改就有了,详细看参考资料地址
----------------------------------
大图上传完了,调用这个方法,最后一个参数是是否按同比例缩小
/*
* 生成微缩图
* */
public void createSmallImages(String fromImg,String toImg,int width,int height,boolean scale)
{
try{
BufferedImage fromBI = ImageIO.read(new FileInputStream(fromImg));
if(scale==true)//同比例缩小
{
float xy = (float)width/(float)height;
if(((float)fromBI.getWidth()/fromBI.getHeight())>xy)
{
xy =(float)width/(float)fromBI.getWidth();
}else{
xy =(float)height/(float)fromBI.getHeight();
}
if(xy>1){//图片变大则用原图大小
width = fromBI.getWidth();
height = fromBI.getHeight();
}else{
width = (int)(xy*fromBI.getWidth());
height =(int)(xy*fromBI.getHeight());
}
}
BufferedImage toBI = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = toBI.createGraphics();
g2.drawImage(fromBI.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
g2.dispose();
ImageIO.write(toBI, "JPG", new File(toImg));
}catch(Exception e)
{
e.printStackTrace();
}
}
参考资料: http://flatfish2000.javaeye.com/blog/437453
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询