Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.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
RESTfulWebService-文件下载_Rest - Fatal编程技术网

RESTfulWebService-文件下载

RESTfulWebService-文件下载,rest,Rest,当我想使用RestFul WebService和jersy下载文本文件(名称为demo.txt)并通过访问它时,我得到了以下错误: HTTP状态500-c:\demo.txt(系统找不到指定的文件),而我在c:drive中有demo.txt 我的代码是: @Path("/file") public class FileService { @GET @Path("/download") @Produces("text/plain") public Response getFile(){

当我想使用RestFul WebService和jersy下载文本文件(名称为demo.txt)并通过访问它时,我得到了以下错误: HTTP状态500-c:\demo.txt(系统找不到指定的文件),而我在c:drive中有demo.txt 我的代码是:

@Path("/file")
public class FileService {

@GET
@Path("/download")
@Produces("text/plain")
public Response getFile(){
    File file=new File("c:\\demo.txt");
    ResponseBuilder builder=Response.ok((Object)file);
    builder.header("Content-Disposition","attachment; filename=\"test1.txt\"");
    return builder.build();
}
}

请帮我提前谢谢

几天前我也在讨论同样的问题。 最好的方法是将文件转换为字节[],类似于

byte[]buffer=IOUtils.toByteArray(is)

其中“is”是要下载并替换代码的文件的输入流对象,如下所示:

@Path("/file")
public class FileService {

@GET
@Path("/download")
@Produces("text/plain")
public Response getFile(){
    ResponseBuilder builder=Response.ok((Object)buffer);
    builder.header("Content-Disposition","attachment; filename=\"test1.txt\"");
    return builder.build();
}
}
这很有效。试着让我知道