【发布时间】:2014-03-08 10:19:28
【问题描述】:
我想使用 java 通过 REST 发送特定文件夹中的文件夹或文件列表作为对客户端的响应。我的服务器端是 EJB。
我正在尝试使用以下代码。但是遇到了 FileNotFoundException
@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getFiles() {
File file = new File("C:\Documents and Settings\Administrator\Desktop\MyFolder");
return Response.ok(file, MediaType.APPLICATION_OCTET_STREAM).build();
}
尝试使用以下代码。但是得到了 NullPointerException
@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getFiles() {
File file = new File("C:\Documents and Settings\Administrator\Desktop\MyFolder");
ResponseBuilder response = Response.ok(file.listFiles());
response.header("Content-Disposition", "inline; filename=units.zip");
return response.build();
}
在 Windows 机器上工作。
谁能给我任何建议或要遵守的工作代码示例?
【问题讨论】:
-
由于从
"/path of the folder"中没有找到任何内容,也许您可以将您使用的实际字符串添加到问题中? -
在我的手机上,但您需要
getResource来获取应用内打包的文件。 -
@t0mppa:请找到更新后的问题
-
您必须明确压缩您的文件夹。 JAX-RS 不会为您创建 zip 文件。查看 java.util.zip 包。
-
您无法发送文件夹。那是不可能的。试着记住上次从网站下载文件夹的时间。你从来没有这样做过,因为这根本不可能。您可以下载文件。这些文件可以是压缩文件。但是你不能下载文件夹。
标签: java file rest response httpresponse