在服务器之间传递图像以进行远程保存

发布于 2024-12-07 08:22:33 字数 679 浏览 0 评论 0原文

我的 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 控制器中,我使用以下方法获取 DataInputString:

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

执行上述所需操作的简单方法是什么? 如何在 Spring 应用程序中将其转换为 BufferedImage,将其 POST 到 Jersey 应用程序,执行所需的操作并保存图像?

如果可以的话,如何将 DataInputStream 转换为 BufferedImage?

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

つ低調成傷 2024-12-14 08:22:33

由于没有答案...

  1. 我从提交的表单数据中获取了字节数组
  2. 发送一个对象,其中 byte[] 作为 REST 服务器的属性之一
  3. 在服务器端,我将 byte[] 转换为我缩放的 BufferedImage(使用 ImgScalr API)按照要求,并保存。

@路径(“/图像”)
公共类 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();     
}

}

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();     
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文