使用Jersey和RestyGWT下载文件
嗨,通过将列表作为参数传递,可以从服务器下载文件吗 用RestyGWT和Jersey 1.7 在服务器端,我有以下web服务使用Jersey和RestyGWT下载文件,gwt,jersey,Gwt,Jersey,嗨,通过将列表作为参数传递,可以从服务器下载文件吗 用RestyGWT和Jersey 1.7 在服务器端,我有以下web服务 @POST @Produces(MediaType.APPLICATION_OCTET_STREAM) @Path("/download") public Response downloadFiles(@Context HttpServletResponse response, List<FileInfo> files) { ZipFile zip =
@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;
}
@POST
@产生(MediaType.APPLICATION\u八位字节\u流)
@路径(“/下载”)
公共响应下载文件(@Context-HttpServletResponse,列表文件){
ZipFile zip=null;
字符串uuid=uuid.randomUUID().toString();
setHeader(“内容处置”、“附件;文件名=”
+uuid+“.zip”);
试一试{
zip=新ZipFile(response.getOutputStream());
文件f=新文件(ConfigurationLoader.getRealPath(“/logo.png”);
zip.addFile(f,“”);
zip.getOutputStream().flush();
zip.getOutputStream().close();
}捕获(例外e){
e、 printStackTrace();
}
返回null;
}
当我在浏览器中键入localhost:8080/Url/download时,它可以工作,但是如何使用Resty Gwt或通过Window.open()下载文件呢
我想使用POSTnot 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;
}
@POST
@生成(“应用程序/zip”)
@路径(“/下载”)
公共void下载文件(列表文件、方法回调响应);
私有静态最终资源=新资源(
GWT.getHostPageBaseURL()+“rest/files”);
公共静态最终FileRestService get(){
if(实例==null){
instance=GWT.create(FileRestService.class);
((RestServiceProxy)实例).setResource(资源);
}
返回实例;
}
但是它不起作用,我也找不到在restygwt中下载文件的例子一般来说(*)你不能使用ajax下载文件,所以你必须使用窗口.open()
或iframe
来要求用户将文件另存为
请看一下我在此查询中的回答:
当然,使用iframe不能使用POST,但是可以编写一个循环,在每次加载最后一个iframe时请求不同的文件。用户将被要求下载文件的次数
您可以使用FormPanel
,然后添加一些隐藏参数并将其发布到服务器。FormPanel在隐藏的iframe中显示响应,因此设置适当的标题(内容类型content disposition)可以下载文件并要求用户另存为。我会压缩文件集,这样用户可以保存或打开内容,因为更多操作系统都有可视化压缩文件的实用程序
(*)使用XHR可以下载文件,但需要一种处理内容并向用户显示的方法。它通常用于文本文件html、txt、xml等。请查看以接收和处理二进制数据。但是,您不能在用户的文件系统中创建文件