1

嗨,是否可以通过将列表作为参数传递来从服务器下载文件

使用RestyGWTJersey 1.7?

在服务器端,我有以下 Web 服务

@POST
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Path("/download")
public Response downloadFiles(@Context HttpServletResponse response, List<FileInfo> files) {
    ZipFile zip = null;

    String uuid = UUID.randomUUID().toString();
    response.setHeader("Content-Disposition", "attachment; filename="
            + uuid + ".zip");
    try {
        zip = new ZipFile(response.getOutputStream());
        File f = new File(ConfigurationLoader.getRealPath("/logo.png"));
        zip.addFile(f, "");
        zip.getOutputStream().flush();
        zip.getOutputStream().close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

当我在浏览器中输入localhost:8080/Url/download时,它可以工作,但是我如何使用 Resty Gwt 或通过 Window.open() 下载文件?

我想使用POST而不是 GET,所以我可以传递可序列化对象的列表,例如: 列表文件

我在客户端尝试了 RestyGWT:

@POST
@Produces("application/zip")
@Path("/download")
public void downloadFiles(List<FileInfo> files, MethodCallback<Response> response);

private static final Resource resource = new Resource(
                GWT.getHostPageBaseURL() + "rest/files");

        public static final FileRestService get() {
            if (instance == null) {
                instance = GWT.create(FileRestService.class);
                ((RestServiceProxy) instance).setResource(resource);
            }
            return instance;
        }

但它不起作用,我找不到有关在 restygwt 中下载文件的示例

4

1 回答 1

2

In general(*) you cannot use ajax to download files, so you have to use a Window.open() or an iframe to ask the user to save the file as.

Take a look to my response in this query: Window.open in GWT not open correctly with in a call back function

Of course using iframes you cannot use POST, but you could code a loop to ask for a different file each time the last iframe is loaded. The user will be asked as many times as files to download.

You could use a FormPanel though, then add some hidden parameters and POST it to the server. FormPanel displays the response in a hidden iframe, so setting the appropriate headers (content-type content-disposition) you could download a file and ask the user to save as. I would zip the set of files so the user can save or open the content since more OS have utilities to visualize compressed files.

(*) using XHR you could download files, but you need a way to process the content and display it to the user. It's normally used for text files html, txt, xml, etc. Take a look to the html5 api to receive and process binary data. You cannot create files in the user's filesystem though.

于 2013-07-18T16:45:04.980 回答