加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 教程 > 正文

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

发布时间:2023-05-24 13:47:40 所属栏目:教程 来源:
导读:<%@ page contenttype="text/html;charset=gb2312" %>

<%@ page language="java" import="java.util.*" %>

<%@ page language="java" import="java.io.*" %>

<%@ page language="j
<%@ 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 xmlns="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>
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!