Reputation: 1237
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.
Upvotes: 0
Views: 405
Reputation: 1237
Since there was no answer ...
@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();
}
}
Upvotes: 2