Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Java通过REST向客户端发送文件夹作为响应?_Java_File_Rest_Response_Httpresponse - Fatal编程技术网

如何使用Java通过REST向客户端发送文件夹作为响应?

如何使用Java通过REST向客户端发送文件夹作为响应?,java,file,rest,response,httpresponse,Java,File,Rest,Response,Httpresponse,我想使用java通过REST向客户机发送特定文件夹中的文件夹或文件列表作为响应。我的服务器端是EJB 我正在尝试以下代码。但得到了FileNotFoundException @GET @Produces(MediaType.APPLICATION_OCTET_STREAM) public Response getFiles() { File file = new File("C:\Documents and Settings\Administrator\Desktop\MyFolder"

我想使用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机器中工作


有谁能给我一些建议,或是要遵循的工作代码示例吗?

在文件名中使用正斜杠或双反斜杠


另外,不要期望文件夹自动压缩。您必须创建ZIP存档并将其传递给
响应

,因为在
“/path of the folder”
中找不到任何内容,也许您可以将实际使用的字符串添加到问题中?在我的手机上,但是你需要
getResource
来获取打包在你的应用程序中的文件。@t0mppa:请查找更新的问题,你必须明确地压缩你的文件夹。JAX-RS不会为您创建zip文件。看看java.util.zip包,你不能发送文件夹。那是不可能的。请记住上次从网站下载文件夹的时间。你从来没有这样做过,因为这根本不可能。你可以下载文件。这些文件可以是压缩文件。但是你不能下载文件夹。我只是拿互联网为例。但实际上我不想压缩我的文件夹并发送给客户。我想要的是,我需要客户端文件夹中的相同文件夹/文件