使用Jersey传输文件和数据 [英] Trasferring files and data with Jersey

查看:126
本文介绍了使用Jersey传输文件和数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试与Jersey合作开发一些休息服务,以上传和下载文件(类似于文件管理器).如果我的服务仅将File类生成/消费为"application/octet-stream",如下面的代码所示,则它们起作用.

I'm trying to develop some rest services with Jersey to upload and download files (something like a file manager). If my services produce/consume only File class as "application/octet-stream", like in the code below, they work.

@GET
@Produces("application/octet-stream")
public File getFile(String id) {
    try {
        File file = new File(System.getProperty("user.home"), id);
        return file;
    } catch (FileNotFoundException ex) {
        Logger.getLogger(GenericResource.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(GenericResource.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

但是,如果我尝试传输包含File字段和一些其他数据(示例中的FileEnvelope)的自定义对象,则会出现错误.

But if I try to transfer a custom object that contains a File field and some other data (FileEnvelope in the sample) I obtain an error.

@GET
@Produces("application/octet-stream")
public FileEnvelope getXml(String id) {
    try {
        File file = new File(System.getProperty("user.home"), id);
        FileEnvelope fileEnvelope = new FileEnvelope(file, "text");
        return fileEnvelope;
    } catch (FileNotFoundException ex) {
        Logger.getLogger(GenericResource.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(GenericResource.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

错误是

原因:com.sun.jersey.api.MessageException:Java类com.mycompany.restdemo.FileEnvelope和Java类型类com.mycompany.restdemo.FileEnvelope以及MIME媒体类型application/octet的消息正文编写器找不到流

Caused by: com.sun.jersey.api.MessageException: A message body writer for Java class com.mycompany.restdemo.FileEnvelope, and Java type class com.mycompany.restdemo.FileEnvelope, and MIME media type application/octet-stream was not found

我错了哪里?这是处理此案的正确方法吗?我的客户不能是泽西客户".

Where I'm wrong? Is this the right way to manage this case? My client could not be a "Jersey client".

推荐答案

Jersey不知道如何将域对象序列化为八位字节流,除非您告诉它如何.在这种情况下,如果要包括文件数据以外的其他信息,则应考虑应如何期望客户端读取它.您可以:

Jersey has no idea how to serialize your domain object into an octet-stream unless you tell it how. In this case if you want to include extra information beyond the file data you should consider how the client should be expected to read it. You could:

  1. 通过创建自己的

  1. Embed the information directly in the octet stream by creating your own MessageBodyWriter. The client would need to know where to look for this information in the resulting file.

使用

Include the information as part of the HTTP response header using ResponseBuilder. The client would just need to know which response headers to check for the information.

这篇关于使用Jersey传输文件和数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆