RESTfulWebService-文件下载
当我想使用RestFul WebService和jersy下载文本文件(名称为demo.txt)并通过访问它时,我得到了以下错误: HTTP状态500-c:\demo.txt(系统找不到指定的文件),而我在c:drive中有demo.txt 我的代码是: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(){
@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();
}
}
这很有效。试着让我知道