• 在线图片压缩后以ImageIO 流的形式 设置大小显示指定页面


    1、Servlet   代码

    public class ZoomImgServlet extends HttpServlet implements Servlet {

        public void init(ServletConfig conf)throws ServletException {}
        public void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            int width=352,height=233;//这是设置图片显示的宽和高
            String srcImgPath=“http://pic.to8to.com/attch/day_160218/20160218_d968438a2434b62ba59dH7q5KEzTS6OH.png”;
            resp.setDateHeader("Expires", 0L);
            resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
            resp.addHeader("Cache-Control", "post-check=0, pre-check=0");
            resp.setHeader("Pragma", "no-cache");// 设置响应头信息,不缓存此内容
            resp.setContentType("image/jpeg");
            URL url = new URL(srcImgPath);  
            //打开链接  
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
            //设置请求方式为"GET"  
            conn.setRequestMethod("GET");  
            //超时响应时间为5秒  
            conn.setConnectTimeout(5 * 1000);  
            InputStream inStream = conn.getInputStream();
            byte[] data = null;
            ServletOutputStream out=null;
            try {
                data = readInputStream(inStream);
                ByteArrayInputStream bin = new ByteArrayInputStream(data);  
                Image srcImg = ImageIO.read(bin);  
                BufferedImage buffImg = null;  
                buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
                buffImg.getGraphics().drawImage(srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0,0, null);  
                out = resp.getOutputStream();
                ImageIO.write(buffImg, "PNG", out);
                out.flush();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                out.close();
            }
        }
        
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            super.doGet(req, resp);
        }
        
        
         private static byte[] readInputStream(InputStream inStream) throws Exception{  
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
            //创建一个Buffer字符串  
            byte[] buffer = new byte[1024];  
            //每次读取的字符串长度,如果为-1,代表全部读取完毕  
            int len = 0;  
            //使用一个输入流从buffer里把数据读取出来  
            while( (len=inStream.read(buffer)) != -1 ){  
                //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度  
                outStream.write(buffer, 0, len);
            }
            //关闭输入流  
            inStream.close();  
            //把outStream里的数据写入内存  
            return outStream.toByteArray();  
        }  
    }

    2、web.xml 配置

      <servlet>
        <servlet-name>zoomImgServlet</servlet-name>
        <servlet-class>com.test.servlet.ZoomImgServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>zoomImgServlet</servlet-name>
        <url-pattern>/zoomImg.jpg</url-pattern>
      </servlet-mapping>

    3、页面调用

    <img src="${path}/zoomImg.jpg" />

  • 相关阅读:
    重载运算符 && 构造函数 的写法
    2019 ICPC Asia Xuzhou Regional
    中国剩余定理
    求逆元
    Exgcd
    Leading Robots
    大家好
    AtCoder Grand Contest 047 部分题解
    CodeForces 1389E Calendar Ambiguity 题解
    CodeForces 1380F Strange Addition 题解
  • 原文地址:https://www.cnblogs.com/laotan/p/5333619.html
Copyright © 2020-2023  润新知