问题描述
我正在使用GWT(Google Web Toolkit)制作一个网站。
我需要向用户显示一个表,并让用户下载表的内容。
在客户端,用户如何下载一个文件,当他们按下下载按钮?
下载按钮有一个 onClick()
监听器。客户端类扩展 Composite
。
我试图让类扩展 HttpServlet
,但它变得太复杂了。
我已经在这里阅读帖子:
但是我仍然不知道如何在客户端向用户提供可下载的文件。
您真的需要区分GWT客户端Java代码和服务器端java代码。 / p>
在GWT Java代码的客户端
String url = GWT.getModuleBaseURL()+downloadService?fileInfo1 =+ fileInfo1;
Window.open(url,_blank,status = 0,toolbar = 0,menubar = 0,location = 0);
在非gwt Java代码中的服务器端 -
在web.xml中
< servlet>
< servlet-name> downloadService< / servlet-name>
< servlet-class> AAA.BBB.CCC.DownloadServlet< / servlet-class>
< / servlet>
< servlet-mapping>
< servlet-name> downloadService< / servlet-name>
< url-pattern> /< gwtmodulename> / downloadService< / url-pattern>
< / servlet-mapping>
在服务器包代码中,一个servlet
public class DownloadServlet extends HttpServlet {
protected void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException
{
String fileName = req.getParameter( fileInfo1);
int BUFFER = 1024 * 100;
resp.setContentType(application / octet-stream);
resp.setHeader(Content-Disposition:,attachment; filename =+\+ fileName +\);
ServletOutputStream outputStream = resp.getOutputStream();
resp.setContentLength(Long.valueOf(getfile(fileName).length()).intValue());
resp.setBufferSize(BUFFER);
//您的IO代码到这里创建一个文件并设置为outputStream //
}
}
确保将您的文件内容推送到 ** outputStream **
。
I'm using GWT(Google Web Toolkit) to make a website.I need to show a table to the user, and let the user download the contents of the table.
On the client side, how can a user download a file when they press the "download" button?
The "Download" button has an onClick()
listener. And the client side class extends Composite
.
I've tried to make the class extend HttpServlet
, but it becomes too complicate.
I already read posts here:
- http://www.mkyong.com/java/how-to-download-file-from-website-java-jsp/
- How to use GWT when downloading Files with a Servlet?
But I still don't know how can I provide downloadable file to the user on the client side.
You REALLY need to distinguish between GWT client side java code and server side java code.
On the client side in your GWT Java Code
String url = GWT.getModuleBaseURL() + "downloadService?fileInfo1=" + fileInfo1;
Window.open( url, "_blank", "status=0,toolbar=0,menubar=0,location=0");
On server side in your non-gwt Java code-
In web.xml
<servlet>
<servlet-name>downloadService</servlet-name>
<servlet-class>AAA.BBB.CCC.DownloadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>downloadService</servlet-name>
<url-pattern>/<gwtmodulename>/downloadService</url-pattern>
</servlet-mapping>
In server package code a servlet
public class DownloadServlet extends HttpServlet{
protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException
{
String fileName = req.getParameter( "fileInfo1" );
int BUFFER = 1024 * 100;
resp.setContentType( "application/octet-stream" );
resp.setHeader( "Content-Disposition:", "attachment;filename=" + "\"" + fileName + "\"" );
ServletOutputStream outputStream = resp.getOutputStream();
resp.setContentLength( Long.valueOf( getfile(fileName).length() ).intValue() );
resp.setBufferSize( BUFFER );
//Your IO code goes here to create a file and set to outputStream//
}
}
Ensure you push your file contents to **outputStream**
.
这篇关于用户如何在客户端下载文件(Google Web Toolkit)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!