开发者

Passing Image between servers for remote saving

My Spring application passes image file to Jersey application to get rid of all image manipulation tasks.

On receiving image, the Jersey application should save the image after several manipulations (crop, resize etc) and return image url.

For this, the Spring application has the below form in a JSP file:

<form method="POST" action="/asdf" enctype="multipart/form-data">
    <input type="file" name="fstream"></input>
    <input type="submit" value="Upload"/>
</form>开发者_运维技巧;

In my spring controller, I get DataInputString using:

DataInputStream in = new DataInputStream(request.getInputStream());

What would be an easy approach to carry out the desired action mentioned above? How about converting it to BufferedImage in Spring app, POST it to the Jersey app, carry out required manipulations and save the image?

If that's fine, how do I convert DataInputStream to BufferedImage?

Thanks in advance.


Since there was no answer ...

  1. I obtained byte array from submitted form data
  2. Sent an object with byte[] being one of the attribute to REST server
  3. On server side, I converted byte[] to BufferedImage which I scaled (using ImgScalr API) as required, and saved it.

@Path("/image") public class ImageController {

@PUT
@Path("upload/{fileName}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response upload(@PathParam("fileName") String fileName, FileBytes fileBytes){
    byte[] bytearray = fileBytes.getFileBytes();
    int len = bytearray.length;
    if(len>0){
        try {
            int width=0, height=0, edge=0, px1=0, px2=0;
            InputStream in = new ByteArrayInputStream(bytearray);
            BufferedImage image = ImageIO.read(in);

            File file = new File(Constants.PATH_TO_IMAGES+Constants.PATH_ORIGINAL+fileName+".jpg");
            ImageIO.write(image, "png", file); //saving original image

            width = image.getWidth();
            height = image.getHeight();

                            //scaling as required
            if(height>width){
                px2 = (height-width)/2+1;
                edge = width;
            }else if(width>height){
                px1 = (width-height)/2+1;
                edge = height;
            }else{
                edge = width;
            }

                            //using ImgScalr API to get scaled image
            image = image.getSubimage(px1, px2, edge, edge);                
            image = Scalr.resize(image, 120);
            file = new File(Constants.PATH_TO_IMAGES+Constants.PATH_THUMBNAIL+fileName+".jpg");
            ImageIO.write(image, "png", file); //saving scaled image
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return Response.status(Status.OK).entity("Filename:"+fileName).build();     
}

}

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜