2012-08-22 58 views
1

我想從客戶端發送文件到服務器。formpanel.submit不提交GWT服務器上的文件

我的代碼:

客戶端

private FormPanel getFormPanel() { 
    if (formPanel == null) { 
     formPanel = new FormPanel(); 
    formPanel.setMethod(FormPanel.METHOD_POST); 
     formPanel.setEncoding(FormPanel.ENCODING_MULTIPART); 
     formPanel.setAction(GWT.getHostPageBaseURL() +"UploadFileServlet"); 
     formPanel.setWidget(getFlexTable_1()); 

       System.out.println(GWT.getHostPageBaseURL() +"UploadFileServlet"); 
    } 
    return formPanel; 
} 

getFlexTable_1()

flexTable.setWidget(1, 1, getFileUpload()); 

getFileUpload()

private FileUpload getFileUpload() { 
    if (fileUpload == null) { 
     fileUpload = new FileUpload(); 
     fileUpload.setName("upload"); 
    } 
    return fileUpload; 
} 

private Button getAddButton() { 
     if (addButton == null) { 
      addButton = new Button("ADD"); 
      addButton.addClickHandler(new ClickHandler() { 
       public void onClick(ClickEvent event) { 
           formPanel.submit(); 
     } 
     }); 
    } 
return addButton; 

}

在服務器端

public class CmisFileUpload extends HttpServlet implements Servlet{ 

    protected void doGet(HttpServletRequest request, 
      HttpServletResponse response) throws ServletException, IOException { 
     doPost(request, response); 
    } 

    protected void doPost(HttpServletRequest request, 
      HttpServletResponse response) throws ServletException, IOException { 

     byte[] buffer = new byte[115200];// 
     String fileName = null; 
     String mimetype = null; 
     String majorVersion = null; 
     InputStream stream = null; 
     System.out.println("ServletWorking Fine"); 
} 

現在,當我選擇一個文件,然後單擊添加按鈕,我看不到服務器端的輸出,該代碼System.out.println("ServletWorking Fine");

關於cl的輸出System.out.println(GWT.getHostPageBaseURL() +"UploadFileServlet"); ient一邊是

http://127.0.0.1:8888/UploadFileServlet 

,當我直接在瀏覽器中使用這個網址我得到服務器端輸出System.out.println("ServletWorking Fine"); **


編輯

我創建了一個更Web應用程序的文件上傳

public class Uploadfile implements EntryPoint { 

    FormPanel uploadForm = new FormPanel(); 
    public void onModuleLoad() { 

     HorizontalPanel horizontalPanel = new HorizontalPanel(); 

     uploadForm.setAction(GWT.getHostPageBaseURL() +"UploadFileServlet"); 

     uploadForm.setEncoding(FormPanel.ENCODING_MULTIPART); 
     uploadForm.setMethod(FormPanel.METHOD_POST); 
     horizontalPanel.add(uploadForm); 

     // Create a panel to hold all of the form widgets. 
     VerticalPanel panel = new VerticalPanel(); 
     uploadForm.setWidget(panel); 

     FlexTable flexTable = new FlexTable(); 
     panel.add(flexTable); 

     // Create a FileUpload widget. 
     FileUpload upload = new FileUpload(); 
     upload.setName("uploadFormElement"); 
     flexTable.setWidget(2, 3, upload); 
     // panel.add(upload); 

     // Add a 'submit' button. 
     Button uploadSubmitButton = new Button("Submit"); 
     panel.add(uploadSubmitButton); 

     uploadSubmitButton.addClickHandler(new ClickHandler() { 

      public void onClick(ClickEvent event) { 
       // TODO Auto-generated method stub 
       uploadForm.submit(); 
      } 
     }); 
     uploadForm.addFormHandler(new FormHandler() { 
      public void onSubmit(FormSubmitEvent event) { 
      } 
      public void onSubmitComplete(FormSubmitCompleteEvent event) { 
      Window.alert(event.getResults()); 
      } 
     }); 
     RootPanel.get().add(horizontalPanel); 
    } 
} 

服務器

protected void doGet(HttpServletRequest request, 
      HttpServletResponse response) 
          throws ServletException, IOException { 
     doPost(request, response); 
    } 

    protected void doPost(HttpServletRequest request, 
      HttpServletResponse response) 
          throws ServletException, IOException { 

     System.out.println("working fine"); 
    } 

此代碼工作正常

根據我有代碼沒有什麼區別。

PLZ告訴我爲什麼formpanel.submit工作不正常。

Plz help。

+0

你在你的瀏覽器開發者工具看到(。 Firebug或Chrome開發工具或其他瀏覽器中的等價物),在網絡選項卡中?是否發送任何內容?預期的請求?到哪個URL? –

+0

@ThomasBroyer我使用的是Firebug,在networkTab中,網址是127.0.0.1:8888/UploadFileServlet和時間軸只是工作。沒有其他 – NewCodeLearner

+0

@ThomasBroyer在networkTab的網址是127.0.0.1:8888/UploadFileServlet和時間線正常工作。沒有其他 – NewCodeLearner

回答

5

hide()方法關閉窗口???如果是,則

刪除移動代碼hide();formPanel.submit();

hide()使用FormHandler。對於如

uploadForm.addFormHandler(new FormHandler() { 

    public void onSubmitComplete(FormSubmitCompleteEvent event) { 
     hide(); 
    } 

    public void onSubmit(FormSubmitEvent event) { 

    } 

}); 

原因:FormPanel不得是分離(即從其父刪除,直到提交完成,否則,提交的通知將失敗

+0

This works ..這是小部件是提交類後隱藏的對話框.. 感謝解釋.. :) – NewCodeLearner

0

爲什麼你已經映射GET方法進行文件上傳。 GET請求方法用於在瀏覽器中輸入的網址。刪除GET請求地圖,它將工作。

對於POST請求地圖,則可以使用用於MultipartFile RequestParam如下

protected void uploadFileAndReconcilePayout1(@RequestParam("documentUpload") MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException { 
     //code for file working 

    } 
+0

是的我underStand.Any方法doGET方法是調用doPost方法。 serverSide沒有問題。 formPanel有問題。我可以這樣說,因爲相同的代碼在另一個webApplication中工作。 – NewCodeLearner

相關問題