2013-08-20 67 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();

您将在“文件”变量中以编码格式获取所有文件内容。