⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imagesize.java

📁 一本jsp自学教材
💻 JAVA
字号:
package test;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.sun.image.codec.jpeg.*;
import java.awt.image.*;
import java.awt.*;
import javax.imageio.ImageIO;

public class ImageSize extends HttpServlet {
    ServletConfig config=null;
    private int old_w=0;  //定义原图片宽
    private int old_h=0;  //定义原图片高
    private int new_w=0;  //定义新图长
    private int new_h=0;                           
    private float imagesize;
    public void init(ServletConfig config)throws ServletException
     {
       super.init(config);      
       this.config=config;
       String isize=config.getInitParameter("imagesize");
       imagesize=Float.parseFloat(isize);
     }
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {        
        response.setContentType("image/jpeg;charset=GBK");
        
        try{
           String path=request.getRealPath("chart.jpg");
           File file = new File(path); 

           Image src =ImageIO.read(file);    //构造Image对象
           float tagsize=200;
           old_w=src.getWidth(null);    //得到原图片宽度
           old_h=src.getHeight(null);    //得到原图片高度

           new_w=Math.round(old_w*imagesize); //计算新图宽宽 
           new_h=Math.round(old_h*imagesize); //计算新图高宽
           BufferedImage image = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
           //重新绘出缩小后的图
           image.getGraphics().drawImage(src,0,0,new_w,new_h,null); 
          //获取输出流
         ServletOutputStream newimage=response.getOutputStream();        
         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);       
         encoder.encode(image);    //JPEG编码
         newimage.close(); 
       }  
       catch(Exception e)
          {
            PrintWriter out = response.getWriter();
            out.print(e.toString()); 
          } 
      
    }  
    public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {  
      doGet(request,response);    
    }   
   public void destroy()
    {
     config=null;
    }
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -