在smartGWT中打开/保存文件 [英] open/save file in smartGWT

查看:113
本文介绍了在smartGWT中打开/保存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了RPCService,RPCServiceAsync& RPCServieImpl。在点击一个按钮时,服务器端的服务将被调用,它将从数据库获取数据并创建文件。一旦文件被创建,然后我需要在客户端打开该文件,并需要提示一个对话框打开/保存选项。
我如何实现这个打开文件的一部分。请提供一个方法来实现t ..回复请提前致谢....

@汉姆:我还有一个更多的说明!如何调用这在另一个servlet中的doGet方法,即在我的onmodule加载类中,我有很多独立布局的小部件,并且一个这样的小部件是BUTTON? onclicking这个按钮服务RPCServiceImpl被调用并且所有的操作都完成并且文件被创建在一个serperate函数中(public int GenerateFile(String name)())。如何使该函数调用doGet方法?因为doGet需要请求,响应参数要与它一起传递?请告诉我一种调用该方法的方法。在此先感谢

解决方案

GWT的RPC服务只能将java对象发送回客户端。要将文件发送回用户,您需要使用非GWT Java servlet。以下是我之前用来从存储库提供jpeg图像的代码。

  public class ImageServlet extends HttpServlet {
private final String repository =/ var / images /;

protected void doGet(HttpServletRequest请求,HttpServletResponse响应)
throws ServletException,IOException {
String filename = request.getParameter(file);

//安全性:文件名中的'..'会让偷偷摸摸的用户访问文件
//不在您的存储库中。
filename = filename.replace(..,);

档案档案=新档案(档案库+档案名称);
if(!file.exists())
抛出new FileNotFoundException(file.getAbsolutePath());

response.setHeader(Content-Type,image / jpeg);
response.setHeader(Content-Length,String.valueOf(file.length()));
response.setHeader(Content-disposition,attachment; filename = \+ filename +\);

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
byte [] buf = new byte [1024];
while(true){
int length = bis.read(buf);
if(length == -1)
break;

bos.write(buf,0,length);
}
bos.flush();
bos.close();
bis.close();


Content-disposition:attachment会导致大多数浏览器下载文件而不是显示它,文件名默认为您提供的任何内容。你使用这个servlet的方式是让用户调用你已有的RPCService,它将文件保存到存储库文件夹中。然后,通过诸如 http:// your url之类的URL将它们链接或重定向到此servlet。 domain.com/fileServlet?file=myFile.jpg 的。很显然,使用这种设置时,如果用户可以猜测文件名,用户可以下载其他用户的文件,但存在安全风险。



您可能想要将数据库代码合并你的RPC服务到这个servlet中。不需要将文件保存在服务器上的任何位置,您可以将数据库结果写入response.getOutputStream()或response.getWriter(),其方式与将文件写入文件的方式完全相同,除了结果直接给用户。只要你正确地设置了你的内容标题,用户就不会注意到它们之间的区别。



你不能从另一个servlet调用这个方法,浏览器下载它作为文件是通过正常的HTTP请求访问它。首先,在您的web.xml文件中声明servlet,就像您将使用GWT RPC服务一样:

 < servlet> 
< servlet-name> ImageServlet< / servlet-name>
< servlet-class>
com.package.ImageServlet
< / servlet-class>
< / servlet>
< servlet-mapping>
< servlet-name> ImageServlet< / servlet-name>
< url-pattern> / imageServlet< / url-pattern>
< / servlet-mapping>

现在任何HTTP GET请求都会发送到 http://your.tomcat.server/webapp/imageServlet 将被ImageServlet.doGet()获取。然后在客户端,您可以创建一个正常的HTML链接到该文件:

  new HTML(< a href = '+ GWT.getHostPageBaseURL()+imageServlet?file =+ filename +'>下载< / a>); 

...或者,您应 ClickHandler(我还没有测试过):

pre $ Window.Location.assign(GWT.getHostPageBaseURL()+imageServlet?file =+ filename);


I have implemented RPCService, RPCServiceAsync & RPCServieImpl. On clicking a button a service in server side will be called and it will fetch data from DB and file is created. Once the file is created, then i need to open that file in client side and need to prompt a dialog box with open/save options. how can i implement this opening a file part. pls suggest a way to implement t.. Reply pls.. thanks in advance....

@Hambend : I still have one more clarification !.. how to call this doGet method in another servlet i.e. in my onmodule load class i am having lot of widgets in seperate layout and one such a widget is BUTTON ? onclicking this button service RPCServiceImpl is called and all manipulations are done and file is created in a serperate function (public int GenerateFile(String name)() ) . how to make that function to call doGet method ? since doGet needs request,response parameters to be passed along with it?? pls suggest me a method to call that method. thanks in advance

解决方案

GWT's RPC services are only able to send java objects back to the client. To send a file back to the user you'll need to use a non-GWT java servlet. Here's some code I've used before for serving up jpeg images from a repository.

public class ImageServlet extends HttpServlet {
    private final String repository = "/var/images/";

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        String filename = request.getParameter("file");

        // Security: '..' in the filename will let sneaky users access files
        // not in your repository.
        filename = filename.replace("..", "");

        File file = new File(repository + filename);
        if (!file.exists())
            throw new FileNotFoundException(file.getAbsolutePath());

        response.setHeader("Content-Type", "image/jpeg");
        response.setHeader("Content-Length", String.valueOf(file.length()));
        response.setHeader("Content-disposition", "attachment;filename=\"" + filename + "\"");

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
        byte[] buf = new byte[1024];
        while (true) {
            int length = bis.read(buf);
            if (length == -1)
                break;

            bos.write(buf, 0, length);
        }
        bos.flush();
        bos.close();
        bis.close();
    }
}

"Content-disposition: attachment" should cause most browsers to download the file instead of displaying it, with the filename defaulting to whatever you provide. The way you would use this servlet is to have the user call the RPCService that you already have, which saves the file to the repository folder. Then, you link or redirect them to this servlet with a url such as http://your.domain.com/fileServlet?file=myFile.jpg. Obviously with this setup you have a security risk where users can download other people's files if they can guess the filenames.

What you might like to do is merge the database code from your RPC service into this servlet. There's no need to save the file anywhere on the server, you can take your database results and write them into response.getOutputStream() or response.getWriter() in exactly the same way you would write them to file, except that the result goes straight to the user. As long as you set your content headers correctly the user won't notice the difference.

You can't call this method from another servlet, the only way to make a browser to download it as a file is to access it through a normal HTTP request. First you declare the servlet in your web.xml file like you would a GWT RPC service:

<servlet>
    <servlet-name>ImageServlet</servlet-name>
    <servlet-class>
        com.package.ImageServlet
    </servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>ImageServlet</servlet-name>
    <url-pattern>/imageServlet</url-pattern>
</servlet-mapping>

Now any HTTP GET requests going to http://your.tomcat.server/webapp/imageServlet will get picked up by ImageServlet.doGet(). Then on the client side you can either make a normal html link to the file:

new HTML("<a href='" + GWT.getHostPageBaseURL() + "imageServlet?file=" + filename + "'>download</a>");

...or, you should be able to put this in a ClickHandler (I haven't tested it):

Window.Location.assign(GWT.getHostPageBaseURL() + "imageServlet?file=" + filename);

这篇关于在smartGWT中打开/保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆