【问题标题】:fails to upload file to embedded OpenEJB无法将文件上传到嵌入式 OpenEJB
【发布时间】:2013-01-30 12:22:07
【问题描述】:

我正在使用嵌入式 openejb 运行一组 jax-rs 服务的集成测试。其中之一需要接收二进制文件。请看下面的方法:

@POST
@Path("signOff")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void signOffDeliveries(
    @Encoded @FormParam("signature") File signature) {

}

现在,此方法在 Websphere 上运行良好,但对嵌入式 openejb 运行测试失败。 signature 得到一个包含整个图像二进制数据的文件名(很多乱码)。

现在读取这个文件会得到一个FileNotFoundException,正如预期的那样。那么我的问题是,这是嵌入式 openejb 中的错误,还是我以错误的方式设置测试?顺便说一句,这是测试代码:

@RunWith(ApplicationComposer.class)
public class DeliveryServiceIT {

    private HttpClient httpClient;
    private DeliveryServiceClient client;

    @Configuration
    public Properties config() throws Exception {
        Properties properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
        properties.setProperty(OpenEjbContainer.OPENEJB_EMBEDDED_REMOTABLE, "true");
        return properties;
    }

    @MockInjector
    public Class<?> mockitoInjector() {
        return MockitoInjector.class;
    }

    @Module
    public EjbModule createModule() throws Exception {
        EjbJar ejbJar = new EjbJar();
        ejbJar.addEnterpriseBean(new StatelessBean(DeliveryService.class));
        EjbModule module = new EjbModule(ejbJar);
        return module;
    }

    @Before
    public void setup() {
        //this is where I create the http client that makes the actual http request
    }

    @Test
    public void signOffDeliveries_givenSignatureImageAndDeliveries_expectsValidRequest() throws FileNotFoundException, IOException {
        File f = new File("/signature.png");

        client.signOffDeliveries(file);
        //the client will take the file, get the bytes, create 
        //multipart/form-data request and send the request to the 
        //service method posted above
    }

由于这在我的 websphere 上运行,我认为它要么是我在 (4.5.0) 中运行集成测试的 openejb 版本的问题,要么是测试的设置方式存在问题。

【问题讨论】:

    标签: java jax-rs multipartform-data openejb


    【解决方案1】:

    您希望您的客户端通过 HTTP 发送文件的完整服务器路径?这正是正在发生的事情。在 HTTP POST 正文中编码的表单参数 signature 用作单个参数来构造类 File 的对象。当然找不到文件。

    改用String 并在您的 JAX-RS 资源中构建特定于服务器的文件路径。

    【讨论】:

    • 客户端发送多部分请求,其中包含内容处置作为有效负载的一部分,并提供参数名称和文件名:Content-Disposition: form-data; name="paramName"; filename="fileName"。此外,传输编码指定为Content-Transfer-Encoding: binary,然后是有效负载。不知何故,openejb 将此请求解释为有效负载是文件名的一部分?
    猜你喜欢
    • 2012-04-28
    • 2020-01-08
    • 2017-10-14
    • 2017-02-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2023-01-17
    相关资源
    最近更新 更多