Download file from server to client - w Servlet etc PLEASE?

3,519 views
Skip to first unread message

JohnnyGWT

unread,
Sep 11, 2008, 10:51:34 PM9/11/08
to Google Web Toolkit
I've seen several discussions on how to download a file to the client.
All contain bits of code but no complete examples.

FileUpload is fine & easy using Apache commons stuff.

Can someone PLEASE provide some examples etc for downloading a file to
the client?
In my scenario I have to send a newly created file to the client.
Either this is by a download servlet 'get' method or a URL.

Any full examples would be greatly appreciated.

Thanx in advance

Jason Morris

unread,
Sep 12, 2008, 3:33:34 AM9/12/08
to Google-We...@googlegroups.com
I assume what you want is for the client to have a new file on their hard-drive.
First you'll need a servlet that produces the data. I'm not sure what
data-format you want to work with, so I'm gonna assume a plain text file here
(note, this is all typed directly into my mail client, sorry for any mistakes).

public class MyFileServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException {

resp.setContentType("text/plain");
resp.setHeader("Content-Disposition", "attachment; filename=output.txt");

PrintWriter out = resp.getWriter();
out.println("This is the output content");
out.println("Probably something dynamic should go in here");
}
}


Then you'll want to write the client side to fetch the file.


public class MyEntryPoint implements EntryPoint {
public void onModuleLoad() {
String link = GWT.getModuleBaseURL() + "servlet/myfiledownload";
RootPanel.get().add(new HTML("<a href=\"" + link + "\">Download File</a>"));
}
}


You can also use Window.open(link, "downloadWindow", "");
to download the file from an EventListener.

Finally you'll need to configure the servlet in either your Module.gwt.xml file
(for hosted mode), or in your web.xml file for web mode.

Module.gwt.xml example, add:

<servlet path="/servlet/myfiledownload"
class="your.package.name.here.MyFileServlet" />


web.xml add:

<servlet>
<servlet-name>MyFileServlet</servlet-name>
<servlet-class>your.package.name.here.MyFileServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>MyFileServlet</servlet-name>
<url-pattern>/your.package.name.here/servlet/myfiledownload</url-pattern>
</servlet-mapping>


Like I show in the web.xml example, you'll need to make sure that the servlet is
bound to the module base directory (where the nocache.html files all live), and
not next to the host HTML page. Another important factor is: the Servlet must
not be in your "client" package, since the GWT compiler shouldn't get hold of it.

Hope this helps.
Jason.

Cerberus

unread,
Sep 12, 2008, 4:57:37 AM9/12/08
to Google Web Toolkit
Hi,

On 12 Sep., 09:33, Jason Morris <lem...@gmail.com> wrote:
> public class MyEntryPoint implements EntryPoint {
>         public void onModuleLoad() {
>                 String link = GWT.getModuleBaseURL() + "servlet/myfiledownload";
>                 RootPanel.get().add(new HTML("<a href=\"" + link + "\">Download File</a>"));
>         }
>
> }
>
> You can also use Window.open(link, "downloadWindow", "");
> to download the file from an EventListener.

If you just want to let the browser pop up a save-as-window, you can
also do the following:

In the HTML where the GWT-application resides put in the following:

<iframe src="" id="__download" style="width:0;height:0;border:0"></
iframe>

(or any other name for the id you want)

and start the download with the following code:

DOM.setElementAttribute(RootPanel.get("__download").getElement(),
"src", link);

Personally I prefer this kind of way because it behaves more like
you're
used to when working with a local application instead of being forced
to
click a link or open a popup containing nothing.


Best regards, Lothar

Joe Cole

unread,
Sep 12, 2008, 6:48:18 PM9/12/08
to Google Web Toolkit
Make sure you use the gwt jsni equivalent to;

var win = Window.open(url, name, options);
if ( win ) return true;
return false;

Otherwise you can't detect when popups are blocked. It will save you
tons of time in user support if you tell them to enable popups if the
window wasn't opened. It amazed me how many users didn't know how to
enable this.

Joe

Craig Mitchell

unread,
Jun 29, 2012, 4:04:20 AM6/29/12
to google-we...@googlegroups.com, Google Web Toolkit
A word of warning with downloading using Window.open, it won't work for IE8 if your site is running over SSL.  <a href...> works fine.

MIKE

unread,
Jul 9, 2012, 3:06:16 PM7/9/12
to google-we...@googlegroups.com, Google-We...@googlegroups.com
Hi,


I got the following error. Any help please ? 

HTTP ERROR: 405

HTTP method GET is not supported by this URL

RequestURI=/name/Download

Powered by Jetty://

Reply all
Reply to author
Forward
0 new messages