2011-09-29 35 views
0

我的Spring應用程序將圖像文件傳遞給Jersey應用程序以擺脫所有圖像處理任務。在服務器之間傳遞圖像以進行遠程保存

在接收圖像時,澤西島應用程序應該在多次操作(裁剪,調整大小等)後保存圖像並返回圖像url。

爲此,Spring應用程序在一個JSP文件中的以下表格:

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

什麼會是一個簡單的方法:

<form method="POST" action="/asdf" enctype="multipart/form-data"> 
    <input type="file" name="fstream"></input> 
    <input type="submit" value="Upload"/> 
</form> 

在我的春天控制器,我開始使用DataInputString執行上面提到的所需操作? 如何在Spring應用程序中將其轉換爲BufferedImage,將其發佈到澤西島應用程序,執行所需的操作並保存圖像?

如果這很好,我該如何將DataInputStream轉換爲BufferedImage?

在此先感謝。

回答

2

由於沒有答案...

  1. 我獲得從提交的表單數據字節數組
  2. 發送與字節[]是所述屬性REST服務器
  3. 在服務器端中的一個的對象,我將byte []轉換爲BufferedImage,我按需縮放(使用ImgScalr API)並保存它。

@Path( 「/圖像」) 公共類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();  
} 

}