How to read a remote file with GWT

I need to read a file located on the server, but I see that it is impossible to use some java library in GWT. what should I do?

+4
source share
3 answers

try requestBuilder !! can this code 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 ); } 
+3
source

Rule: JavaScript cannot read data from a URL that does not have a host name and port that matches those on which JavaScript runs.

In other words: if it is located on another site, you cannot read it directly using JS and, therefore, GWT, which is nothing more than Javascript after compilation.

It applies to data from XMLHttpRequest, frames, and everything you want to name.

This may change in the future, but at the moment this rule is worth it.

With this in mind, there are several workarounds.

1) Name your server RPC or some other mechanism and ask the server to fulfill the request and then send it to the client. Here is an example .

2) There are several hacks that allow JavaScript to access cross-domain sites, just do a Google search on how to get this. Some browsers mean this is dangerous.

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

+1
source

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

Then, when the user clicks the button, for example, you call the servlet with the appropriate 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
source

Source: https://habr.com/ru/post/1305305/


All Articles