我的Spring应用程序将图像文件传递给Jersey应用程序以摆脱所有图像处理任务。
在接收图像时,Jersey应用程序应在多次操作(裁剪,调整大小等)后保存图像并返回图像URL。
为此,Spring应用程序在JSP文件中具有以下形式:
<form method="POST" action="/asdf" enctype="multipart/form-data">
<input type="file" name="fstream"></input>
<input type="submit" value="Upload"/>
</form>
在我的spring控制器中,我使用:
获取DataInputStringDataInputStream in = new DataInputStream(request.getInputStream());
执行上述所需操作的简单方法是什么? 如何在Spring应用程序中将其转换为BufferedImage,将其发布到Jersey应用程序,执行必要的操作并保存图像?
如果没关系,我该如何将DataInputStream转换为BufferedImage?
提前致谢。
答案 0 :(得分:2)
由于没有答案......
@Path( “/图像”) 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();
}
}