简单的图片裁剪服务器

 自己写的一个简单的图片服务器,可以读取FastDFS上的图片,根据参数进行图片裁剪输出到前台

改项目可以上传图片到FastDFS,读取FastDFS上存储的图片,前面可以增加Varnish图片缓存服务器缓解图片裁剪压力

使用一个简单的Servlet实现

package com.imgcut.servlet;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;

//测试URL
//http://127.0.0.1/imgcut/imgcut?width=100&height=100&rotate=120&imgUrl=https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png
public class ImgCutServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private final String WIDTH = "100";
    private final String HEIGHT = "100";
    private final String ROTATE = "90";
    private final String OUTPUTFORMAT = "png";
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException  {
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        
        String width = request.getParameter("width");
        String height = request.getParameter("height");
        String imgUrl = request.getParameter("imgUrl");
        String rotate = request.getParameter("rotate");
        String outputFormat = request.getParameter("outputFormat");
        InputStream is = null;
        ServletOutputStream sos = null;
        int code;
        
        try {
            HttpClient client = new HttpClient();
            GetMethod method = new GetMethod(imgUrl);
            
            code = client.executeMethod(method);
            if(200 == code){
                is = method.getResponseBodyAsStream();
            }
            sos = response.getOutputStream();
            
            Builder builder = Thumbnails.of(is);
            
            //如果没有设置宽度和高度,使用默认值
            if("".equals(width) || width == null){
                width = this.WIDTH;
            }
            
            if("".equals(height) || height == null){
                height = this.HEIGHT;
            }
            builder.size(Integer.parseInt(width), Integer.parseInt(height));
            
            //如果没有设置旋转,使用默认值
            if("".equals(rotate) || rotate == null){
                rotate = this.ROTATE;
            }
            builder.rotate(Integer.parseInt(rotate));
            
            //设置输出格式
            if("".equals(outputFormat) || outputFormat == null){
                outputFormat = this.OUTPUTFORMAT;
            }
            builder.outputFormat(outputFormat);
            
            //输出处理后的图片
            builder.toOutputStream(sos);
            sos.println();
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            response.getWriter().println("项目报错,请查看日志!");
        }
    }
}

测试效果:

 

 

转载于:https://www.cnblogs.com/djoker/p/7062147.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值