读取表单文件并将其内容发送到GWT中的客户端

时间:2011-04-22 14:18:05

标签: gwt

我需要在服务器端读取表单TXT文件并将其内容发送到客户端以打印标签或任何东西,我需要知道我将TXT文件放在服务器包或WAR中的位置以及如何编码?感谢

1 个答案:

答案 0 :(得分:1)

放置文件的位置并不重要。它必须在服务器上,并且php脚本必须能够打开该文件。您可以使用php以下列方式准备文本文件。 然后使用GWT向该文件发出http请求。

阅读文件:

<?php
// get contents of a file into a string
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
echo $contents;
?>

发出http请求:

public class GetExample implements EntryPoint {
  public static final int STATUS_CODE_OK = 200;

  public static void doGet(String url) {
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);

    try {
      Request response = builder.sendRequest(null, new RequestCallback() {
        public void onError(Request request, Throwable exception) {
          // Code omitted for clarity
        }

        public void onResponseReceived(Request request, Response response) {
          String content = response.getText();
        }
      });
    } catch (RequestException e) {
      // Code omitted for clarity
    }
  }

  public void onModuleLoad() {
    doGet("/");
  }
}