2013-08-01 3 views
1

사용자가 이미지를 선택하고 이미지를 선택하고 마지막으로 이미지를 서버에 업로드하는 웹 애플리케이션을 개발 중입니다. 는 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 위젯을 통해 전송됩니다. 그러나 이것은 단지 파일 관리자를 사용하여 사용자가 선택한 파일을 업로드 할 수는 있지만 앱이 수정 한 이미지는 업로드 할 수 없습니다 (크 루프 이미지). 내가 찾고있는 것은 그것을하는 방법이다. –

답변

4

서버에 파일을 업로드하는 방법에 대한 좋은 예를 찾을 수 있습니다 here.

편집이

당신이해야 할 것은, 서버에 이미지를 업로드 클라이언트에서 검색, 클라이언트에서 시각적으로 자르기를 수행 서버로 자르기 매개 변수를 보내는 것입니다, 그리고 마지막으로 실제 일을 서버에서 자르기. 사용자가 이미지를 선택

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

하면, 우리는는 FileUpload와 함께 업로드, 서버에 우리가 디렉토리에 저장 :이 프로젝트는 위에서 언급 한에서 나는 그것이 시작하려면 관리하는 방법입니다

업로드 된 경우 다시 클라이언트에

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

:

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

우리는 우리가 get 메소드를 소요하고 이미지를 반환하는 서블릿을 추가 할 수 있도록 업로드 된 이미지를 검색하는 서비스를 필요 작업이 완료되면 업로드 된 이미지의 복사본을 검색하여 양식 제출 처리기를 추가하려고합니다. 나는 시각 단계에서이 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

좋습니다! 당신의 대답 Amaury에 감사드립니다. 그 옵션을 생각해 봤지만 (클라이언트 서버에서 그랬다.) 클라이언트 사이트에서 그 일을하고 서버 CPU를 릴리스하고 대역폭을 최소화했다. 그러나 그렇게 하기엔 쉽지 않고 우아한 방법이 없다고 생각합니다. 그래서 나는 당신의 해결책을 취할 것입니다. 너보다! –

관련 문제