无法下载附件

时间:2017-04-18 06:44:08

标签: java jersey-2.0

我想用泽西式的休息服务来下载附件。

下面的

是我的代码段:

if(entity != null) {
    ResourceStream stream = new ResourceStream(entity);
    return Response.ok(stream.getResponse(), entity.getMediatype()).header("Content-Disposition", "attachment; filename=\"" + entity.getName() + "\" ").build();
}

以上代码返回以下回复:

Apr 18, 2017 12:06:23 PM org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo
SEVERE: MessageBodyWriter not found for media type=application/pdf, type=class org.glassfish.jersey.message.internal.OutboundJaxrsResponse, genericType=class org.glassfish.jersey.message.internal.OutboundJaxrsResponse.

1 个答案:

答案 0 :(得分:0)

将子资源方法的注释设置为

File file = new File("Location of the file with file name");

创建文件对象,如

if (file.exists()) {
  ResponseBuilder builder = Response.ok(file);
  builder.header("Content-Disposition", "attachment; filename=" + file.getName());
response = builder.build();

并发送这样的回复

return builder;

并返回回复

Name = john
Password = anything

了解更多信息

jersey file upload and download example