2013-08-01 214 views
1

我正在开发一个Web应用程序,用户选择一个图像,对其进行裁剪并最终将其上传到服务器。因此,与一个FileUpload控件,我允许用户选择图像源,获得其路径,并与构造上传GWT中的裁剪图像

Image(java.lang.String url, int left, int top, int width, int height); 

我得到裁剪后的图像对象。

但现在,我不知道如何上传到服务器的图像。有人知道解决方案吗?

+0

您需要指定上传方式。 –

+0

如果你只是在寻找一个文件上传解决方案,你可以使用[Apache Commons File Upload](http://commons.apache.org/proper/commons-fileupload/using.html) – Churro

+0

我知道上传一个唯一的方法文件(在客户端站点中)通过FormPanel和Fileupload小部件。但是,这只允许上传用户使用文件管理器选择的文件,而不是由应用程序修改的图像(crooped图像)。我正在寻找的是一种方法来做到这一点。 –

回答

4

你可以找到一个很好的例子,如何上传文件到服务器here

编辑

你需要做的是将图像上传到服务器,在客户端检索它,做视觉上的裁剪客户端,发送裁剪参数到服务器,最后做实际在服务器中裁剪。这是我能做到它开始从项目上面提到的:

vPanel.add(new Button("Submit", new ClickHandler() { 
    public void onClick(ClickEvent event) { 
      form.submit(); 
    } 
})); 

一旦用户选择图像,我们用的FileUpload上传,并在服务器上我们把它保存在一个目录:

List<FileItem> items = fileUpload.parseRequest(request); 
Iterator<FileItem> iter = items.iterator(); 
while (iter.hasNext()) { 
    FileItem item = (FileItem) iter.next();  
    File file = new File(<images-path>,fileName); 
    Streams.copy(item.getInputStream(),new FileOutputStream(file), true);   
} 

,我们需要有一个服务来获取上传的图片,所以我们增加一个servlet,需要一个GET方法,并返回图像:当上载已

protected void doGet(HttpServletRequest req, 
     HttpServletResponse resp) throws ServletException, 
     IOException 
{ 
    resp.setContentType("image/jpeg"); 
    ServletOutputStream out = resp.getOutputStream(); 
    BufferedInputStream bis= new BufferedInputStream(new FileInputStream(<images-path>+req.getParameter("name"))); 
    BufferedOutputStream bos = new BufferedOutputStream(resp.getOutputStream()); 
    int ch; 
    while((ch=bis.read())!=-1) 
    { 
     bos.write(ch); 
    } 
    bis.close(); 
    bos.flush(); 
    bos.close(); 
    out.close(); 
} 

返回到客户端完成后,我们希望检索上传图像的副本,以便添加表单提交处理程序。我用这gwt cropping library的视觉步:

form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() 
{ 
    public void onSubmitComplete(SubmitCompleteEvent event) 
    { 
     Element label = DOM.createLabel(); 
     label.setInnerHTML(event.getResults()); 
     String result = label.getInnerText(); // result contains the name of the image in the server 
     final GWTCropper crop = new GWTCropper(GWT.getModuleBaseURL()+"image?name="+result); 
     crop.setAspectRatio(1); 
     vPanel.add(crop); 
    } 
} 

现在我们要添加一个裁剪服务,让实际种植在我使用RCP服务做服务器发生:

public class CropServiceImpl extends RemoteServiceServlet implements CropService { 
    public Boolean crop(String name, int x, int y, int width, int height) 
    { 
     try 
     { 
      BufferedImage outImage = ImageIO.read(new File("<images-path>"+name)); 
      BufferedImage cropped = outImage.getSubimage(x, y, width, height); 
      ImageIO.write(cropped, "jpg", new File("<images-path>","cropped"+name)); 
      return true; 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
     return false; 
    } 
} 

最后,返回到客户端,我们调用该方法,我们从裁剪得到的参数的按钮操作中:

vPanel.add(new Button("Crop", new ClickHandler() 
{ 
    public void onClick(ClickEvent event) 
    { 
     cropService.crop(getName(), (int) crop.getSelectionXCoordinate(), 
       (int) crop.getSelectionYCoordinate(), (int) crop.getSelectionWidth(), 
       (int) crop.getSelectionHeight(), new AsyncCallback<Boolean>() 
       { 
        public void onFailure(Throwable arg0) 
        { 
         // something went wrong with the call 
        } 
        public void onSuccess(Boolean arg0) 
        { 
        if (arg0) 
         { 
          // the cropped file now lives in the server 
         } 
         else 
         { 
          // an error happened in the server 
         } 
        } 
       }); 
    } 
})); 

和你去,比较遗憾的是龙埔圣,希望它有帮助。

+0

本示例通过FormPanel使用FileUpload小部件,该小部件不允许动态上载修改后的图像,只是由用户直接选择的图像。 –

+0

@Javier Vallori Amoros我编辑了我的答案,你可能想看看。 – amaurs

+0

好的!谢谢你的回答阿毛里。我曾考虑过这个选择(在服务器上进行操作),但我想在客户端做这项工作,释放服务器CPU,并尽量减少带宽(这意味着更便宜:D)。但我认为这并不是一种简单而优雅的方式,所以我会采取您的解决方案。再比你一次! –