您现在的位置:计算机技术学习网 > 技术中心 > WEB编程 > JSP >

jsp中生成图片缩略图的代码

来源:网络 责任编辑:栏目编辑 发表时间:2013-07-02 02:28 点击:

<%@ page contentType="text/html;charset=gb2312" %>
<%@ page language="java" import="java.util.*" %>
<%@ page language="java" import="java.io.*" %>
<%@ page language="java" import="java.awt.*" %>
<%@ page language="java" import="java.awt.image.*" %>
<%@ page language="java" import="com.sun.image.codec.jpeg.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml(标准化越来越近了)ns="http://www.w3.org/1999/xhtml">
<head>
  <title> </title>
  <meta name="generator" content="editplus" />
  <meta name="author" content="Wang Shiqiang" />
  <meta name="keywords" content="Wang Shiqiang" />
  <meta name="description" content="" />
</head>

<body>
 <%
    response.setContentType("text/html;charset=gb2312");
    request.setCharacterEncoding("gb2312");

    out.print("==================Test for thumbtail picture=============");

    //-----------------------上传完成,开始生成缩略图-------------------------  
    java.io.File file = new java.io.File("E:\1110168154_58348.jpg");//saveurl);        //读入刚才上传的文件
    String newurl="E:\1110168154_58348_min.jpg";//request.getRealPath("/")+url+filename+"_min."+ext;  //新的缩略图保存地址
    Image src = javax.imageio.ImageIO.read(file);                     //构造Image对象
    float tagsize=287;
    int old_w=src.getWidth(null);                                     //得到源图宽
    int old_h=src.getHeight(null); 
    int new_w=0;
    int new_h=0;                            //得到源图长
    int tempsize;
    out.print("<br/>the old width is :"+old_w+" the old height is "+old_h+"<br/>");
    float tempdouble;
    if(old_w>old_h){
     tempdouble=old_w/tagsize;
    }else{
     tempdouble=old_h/tagsize;
    }
    new_w=Math.round(old_w/tempdouble);
    new_h=Math.round(old_h/tempdouble);//计算新图长宽
    out.print("the new width is :"+new_w+" the new height is "+new_h+"<br/>");
    BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
    tag.getGraphics().drawImage(src,0,0,new_w,new_h,null);       //绘制缩小后的图
    FileOutputStream newimage=new FileOutputStream(newurl);          //输出到文件流
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);     
    encoder.encode(tag);                                               //近JPEG编码
     newimage.close();  
    /*
    */
%>
</body>
</html>

    相关新闻>>

      发表评论
      请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
      用户名: 验证码:点击我更换图片
      最新评论 更多>>

      推荐热点

      • JSP与Servlet
      • 自己动手写MiniBBS系列(基本篇)之用户登录
      • JSP取当前日期
      • JDBC 入门(一)
      • 打开一个jsp页面默认查询所有数据,调用action
      • 使用JSP标签库验证用户的输入(2)完
      • WIN98/2000下的jsp服务器
      • 自定义JSP标签(tag)浅议
      • Struts学习傻瓜式入门篇
      网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
      Copyright © 2008-2015 计算机技术学习交流网. 版权所有

      豫ICP备11007008号-1