java 在 smartGWT 中打开/保存文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/3053462/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
open/save file in smartGWT
提问by Karthikeyan
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....
我已经实现了 RPCService、RPCServiceAsync 和 RPCServieImpl。单击按钮时,将调用服务器端的服务,它将从数据库中获取数据并创建文件。创建文件后,我需要在客户端打开该文件,并需要提示一个带有打开/保存选项的对话框。我怎样才能实现这个打开文件部分。请提出一种实现 t 的方法.. 回复请.. 提前致谢....
@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
@Hambend:我还有一个澄清!..如何在另一个 servlet 中调用这个 doGet 方法,即在我的 onmodule 加载类中,我在单独的布局中有很多小部件,一个这样的小部件是 BUTTON ?单击此按钮服务 RPCServiceImpl 被调用,所有操作都完成,文件在一个 serperate 函数中创建 (public int GenerateFile(String name)())。如何使该函数调用 doGet 方法?由于doGet需要请求,响应参数要随之传递??请建议我一种调用该方法的方法。提前致谢
回答by dslh
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.
GWT 的 RPC 服务只能将 java 对象发送回客户端。要将文件发送回用户,您需要使用非 GWT java servlet。这是我以前用于从存储库提供 jpeg 图像的一些代码。
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.
“内容处置:附件”应该使大多数浏览器下载文件而不是显示文件,文件名默认为您提供的任何内容。您将使用此 servlet 的方式是让用户调用您已有的 RPCService,它将文件保存到存储库文件夹中。然后,您使用诸如http://your.domain.com/fileServlet?file=myFile.jpg 之类的 URL 将它们链接或重定向到此 servlet 。显然,通过这种设置,您存在安全风险,如果用户可以猜出文件名,他们就可以下载其他人的文件。
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.
您可能想做的是将来自您的 RPC 服务的数据库代码合并到这个 servlet 中。无需将文件保存在服务器上的任何位置,您可以将数据库结果写入 response.getOutputStream() 或 response.getWriter() ,其方式与将它们写入文件的方式完全相同,只是结果是直接给用户。只要您正确设置了内容标题,用户就不会注意到差异。
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 调用此方法,使浏览器将其下载为文件的唯一方法是通过正常的 HTTP 请求访问它。首先,像在 GWT RPC 服务中一样在 web.xml 文件中声明 servlet:
<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/imageServletwill get picked up by ImageServlet.doGet(). Then on the client side you can either make a normal html link to the file:
现在任何发往http://your.tomcat.server/webapp/imageServlet 的HTTP GET 请求都会被 ImageServlet.doGet() 接收。然后在客户端,您可以创建指向该文件的普通 html 链接:
new HTML("<a href='" + GWT.getHostPageBaseURL() + "imageServlet?file=" + filename + "'>download</a>");
...or, you shouldbe able to put this in a ClickHandler (I haven't tested it):
...或者,您应该能够将它放在 ClickHandler 中(我还没有测试过):
Window.Location.assign(GWT.getHostPageBaseURL() + "imageServlet?file=" + filename);
回答by Romain Hippeau
If the file is text, then you always back back an Object with an array of Strings.
If binary then simply a byte[] in the class would do.
如果文件是文本文件,那么你总是返回一个带有字符串数组的对象。
如果是二进制,那么类中的一个 byte[] 就可以了。

