开发者

how read a remote file with GWT

I need to read a file located on a server but I see that in GWT is not possible use some java library. what I have to do开发者_如何转开发?


try requestBuilder!! this code can help?

        RequestBuilder requestBuilder = new RequestBuilder( RequestBuilder.GET, "yourfile.txt" );
        try {
            requestBuilder.sendRequest( null, new RequestCallback(){
                public void onError(Request request, Throwable exception) {
                    GWT.log( "failed file reading", exception );
                }

                public void onResponseReceived(Request request, Response response) {
                    String result=response.getText();

                }} );
        } catch (RequestException e) {
            GWT.log( "failed file reading", e );
        }


The Rule: JavaScript cannot read data from a URL that doesn’t have a host name and port that matches those of the page the JavaScript is running in.

In other words: If it is on a different site — you can’t read it directly with JS and therefore GWT, which is nothing more than Javascript once compiled.

It applies to data from XMLHttpRequest, frames, and anything else you care to name.

This may change in the future, but for now the rule stands.

With this in mind there are a couple of workarounds.

1) Call your server with RPC or whatever mechanism and have your server do the request and then send it back to the client. Here is a sample.

2) There are several hacks on allowing JavaScript to access cross-domain sites just do a google search on how to get this. Some browsers will flag this as being dangerous.

3) If you are using Firefox and Firefox only it looks like Firefox has the ability to do this, but you will need to enable this manually.


Simply write first a servlet that sends the file located on the server to the user.

Then when the user clicks on a button for instance you call the servlet with the proper parameter.

Here is an excerpt from our servlet implementation

            response.reset();

            response.setContentType("application/octet-stream");
            response.setContentLength(contentLength);
            response.setHeader("Content-disposition", "attachment;
filename=\"" + filename + "\"");
            output = new
BufferedOutputStream(response.getOutputStream());
            int data = input.read();
            while (data != -1)
            {
                output.write(data);
                data = input.read();
            }
            output.flush(); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜