2013-04-24 82 views
0

我發展模式當GWT如何mkdirs。 fileupload工作得很好。但我會做目錄在Linux上/var/wms/year/month/file.jpg。從客戶端這是我的源代碼: 附加組件形成客戶端文件上傳在Eclipse

fileUpload = new SingleUploader(FileInputType.LABEL); 
    fileUpload.setFileInputPrefix("PJ"); 
    fileUpload.addOnFinishUploadHandler(onFinishUploaderHandler); 
    layoutContainerItemRight.add(fileUpload, formData); 

方法是addOnFinishUploadHandler

private IUploader.OnFinishUploaderHandler onFinishUploaderHandler = new IUploader.OnFinishUploaderHandler() { 
    public void onFinish(IUploader uploader) { 
     if (uploader.getStatus() == gwtupload.client.IUploadStatus.Status.SUBMITING) { 
      String month = VisionProperties.getBulan(); 
      String year = DateTimeFormat.getFormat("d-M-yyyy").format(new Date()).split("-")[2]; 
      String strDirectoy = "/var/wms/" + year + "/" + month + "/"; 
      File file = new File(strDirectoy); 
      if (!file.exists()) { 
       file.mkdirs(); 
      } 
     } 

     if (uploader.getStatus() == gwtupload.client.IUploadStatus.Status.SUCCESS) { 
     String msg = uploader.getServerInfo().message; 
     fileName = msg.toString(); 
      if(selectWindow != 2){ 
       img.setUrl("servlet.gupld?show=&fieldname=" + fileName); 
       itemPanel.render(img.getElement()); 
      }else{ 
       tb.setVisible(true); 
       tb.setText("Download File "+uploader.getFileName()); 
      } 
     } 
    } 
}; 

如何使當上傳文件的過程目錄下的文件?

+0

GWT打包機:http://www.vectomatic.org/google-web-toolkit/a-file-api-for-gwt – Spiff 2013-04-24 06:49:58

回答

1

您正試圖在客戶端使用java.io.File,GWT jre emulation中的一組軟件包不支持該端口。

如果您想在客戶端執行此操作,您必須使用舊版瀏覽器不支持的javascript File Api,並且未在gwt-core中實現。使用元素,你可以使用Api只與Chrome,但我不積極。所以最好通過jsni包裝它,它計劃在gwtupload中,但是還沒有時間框架。請注意,使用js文件Api,您不能訪問您的真實文件系統,而是瀏覽器中的虛擬文件系統。要將創建的文件保存在本地文件系統中,您必須使用和iframe進行下載,以便向用戶詢問保存的位置。

否則,如果您想在服務器端執行此項工作,請在您的servlet中重寫executeAction,如果您要擴展UploadAction

+0

我使用executeAction創建目錄到服務器端。如何在我上傳文件之前創建目錄文件夾? – mogi 2013-04-24 10:07:58

+0

上傳前無法創建文件夾。 'UploadAction'重新記錄文件並將它保存在一個臨時文件夾中,然後調用'executeAction',你可以創建dirs,將上傳的文件複製到它,並刪除臨時文件。 – 2013-04-24 15:31:57

0

你不能這樣做在客戶端。您可以通過以下方式在服務器端執行此操作:

  1. 通過另一個rpc/http調用將文件上載到服務器之前。
  2. 你當正在對srever端執行文件上傳的servlet將文件上傳到服務器後。

HTML5文件API被限制爲只讀,甚至在現代瀏覽器的行爲。

參考 - 1. Basic File upload in GWT 2. How to retrieve file from GWT FileUpload component?

+0

文件API是RW,但在瀏覽器的受限區域(虛擬FS)。 – 2013-04-24 08:50:27

+0

如何在上傳文件之前創建目錄文件夾? – mogi 2013-04-24 10:06:15

+0

一旦你即完成文件上傳的客戶端部分之前提交你可以解僱獲得目錄中創建一個額外的RPC調用或HTTP Servlet中調用。我仍然希望在文件上傳servlet的第二個選項中執行此操作。 – SSR 2013-04-24 11:35:47