2013-08-20 66 views
0

我正在使用GWTUpload上傳文件。 我在onFinishHandler中獲取服務器信息,文件名,內容類型等等,但是沒有選擇從服務器到客戶端獲取文件內容。如何使用gwtupload從文件中獲取文件內容

注:我試圖上傳XML文件和Excel文件

我使用GWT 2.4,GXT 3.0.1,GWTUpload 0.6.6

這裏的示例代碼

客戶端代碼 - OnFinishHandler

u.addOnFinishUploadHandler(new OnFinishUploaderHandler() { 
      @Override 
      public void onFinish(IUploader uploader) { 
       if (uploader.getStatus() == Status.SUCCESS) { 

        System.err.println(uploader.getServerResponse()); 

        UploadedInfo info = uploader.getServerInfo(); 
        System.out.println("File name " + info.name); 
        System.out.println("File content-type " + info.ctype); 
        System.out.println("File size " + info.size); 

        System.out.println("Server message " + info.message); 
       } 
      } 
     }); 

servlet代碼

public class GWTFileUploadServlet extends UploadAction { 
    private static final long serialVersionUID = -6742854283091447922L; 

    String fileContent; 
    File uploadedFile; 

    @Override 
    public String executeAction(HttpServletRequest request, 
      List<FileItem> sessionFiles) throws UploadActionException { 
     String response = ""; 
     int cont = 0; 
     for (FileItem item : sessionFiles) { 
      if (false == item.isFormField()) { 
       cont++; 
       try { 
        File file = File.createTempFile("upload-", ".bin"); 
        item.write(file); 

             uploadedFile = file; 
        fileContent = item.getContentType(); 


        response += "File saved as " + file.getAbsolutePath(); 

       } catch (Exception e) { 
        throw new UploadActionException(e.getMessage()); 
       } 
      } 
     } 

     removeSessionFileItems(request); 

     return response; 
    } 

    @Override 
    public void getUploadedFile(HttpServletRequest request, 
      HttpServletResponse response) throws IOException { 
     if (fileContent != null && !fileContent.isEmpty()) { 
      response.setContentType(fileContent); 
      FileInputStream is = new FileInputStream(uploadedFile); 
      copyFromInputStreamToOutputStream(is, response.getOutputStream()); 
     } else { 
      renderXmlResponse(request, response, XML_ERROR_ITEM_NOT_FOUND); 
     } 
    } 

} 

請幫我....

回答

1

你可以看到你已經在文件系統中創建的時候你叫item.write(...)的文件,但它是更好,如果你從InputStreamFileItem您收到並在任何地方寫的內容。例如,如果內容是String可以使用StringWritter得到它:

 InputStream inputStream = item.getInputStream(); 

     StringWriter writer = new StringWriter(); 
     IOUtils.copy(inputStream, writer); 
     String theContentString = writer.toString(); 

將帖子

要獲取客戶端文件的內容,所以你必須從檢索使用任何方法服務器:

  • 正如在你的servlet gwtupload定製的消息,如果文件內容是ASCII:中executeAction使用返回字符串。

  • 執行RequestBuilder調用servlet以使用上傳器url值獲取文件。

  • 使用像RPC一樣的任何GWT ajax機制。

在現代瀏覽器中,您可以在客戶端獲取文件的內容而無需將其發送到服務器端。看看到lib-gwt-file

+0

GwtUpload內部使用IOUtils.copy將輸入流複製到輸出流。我的問題是如何獲取正在複製到outputstream的文件內容。 copyFromInputStreamToOutputStream(is,response.getOutputStream()); 我想在OnFinishHandler裏面的迴應 – Lakshminarayana

+0

你是說你想在客戶端讀取內容嗎? –

+0

是的,我想要的文件內容在客戶端 – Lakshminarayana

0

在你的代碼,你可以只使用

的byte []的文件; file = item.get();

您將在「文件」變量中以編碼格式獲取所有文件內容。