开发者

Java Servlet framework that does things like rencoding images to preferred format etc

开发者 https://www.devze.com 2023-02-13 23:27 出处:网络
Are there any frameworks/libraries that provide servlets/filters etc that handle reencoding on the fly of images.

Are there any frameworks/libraries that provide servlets/filters etc that handle reencoding on the fly of images.

  • interpret the accept headers and output the file, reencoding into the new format if necessary by checking the actual format of the original image file.
  • provide a low and high quality version of an image.
  • re encode an image into ne开发者_开发问答w dimensions. width and height parameters might query string parameters.

I could create versions of the file in all the formats, at upload time but the seems overkill. I would rather lazily create the rencoded file and stick it in a cache if it gets served again etc.


You donot need any framework. Do folowing:

  1. Upload image. See Apache Commons FileUpload
  2. Process the uploaded file using any of
    • Java Advance Imaging
    • Java wrapper for ImageMagick
  3. When processed, provide a link to download.


ImageMagick might be what you're after. http://www.imagemagick.org/script/index.php, there's a java interface for it available here: http://www.jmagick.org/index.html


As Nishants suggested .. I'd use Apache Commons FileUpload, once you get the image .. just convert it to any format you want


You need no framework. Java has all you need:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ImageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("image/png");
        ServletOutputStream output = response.getOutputStream();
        try {
          BufferedImage image = ImageIO.read( new File( "/path/to/image.jpg" ) );
          ImageIO.write(image, "png", output);
        } finally {
          output.close();
        }
    } 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号