Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 在服务器之间传递映像以进行远程保存_Image_Spring_File_Jersey - Fatal编程技术网

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

Image 在服务器之间传递映像以进行远程保存,image,spring,file,jersey,Image,Spring,File,Jersey,我的Spring应用程序将图像文件传递给Jersey应用程序,以摆脱所有图像处理任务 在接收图像时,Jersey应用程序应在多次操作(裁剪、调整大小等)后保存图像,并返回图像url 为此,Spring应用程序在JSP文件中具有以下表单: <form method="POST" action="/asdf" enctype="multipart/form-data"> <input type="file" name="fstream"></input>

我的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应用程序中将其转换为BuffereImage,将其发布到Jersey应用程序,执行所需操作并保存图像

如果可以,如何将DataInputStream转换为BuffereImage


提前谢谢。

因为没有答案

  • 我从提交的表单数据中获取字节数组
  • 向REST服务器发送了一个字节为[]的对象
  • 在服务器端,我将字节[]转换为BuffereImage,并根据需要对其进行缩放(使用ImgScalr API),然后保存它
  • @路径(“/image”) 公共类图像控制器{

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

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