Websphere REST上传 - 不要将上传的文件加载到内存中

时间:2014-09-03 13:11:58

标签: java rest websphere jax-ws apache-wink

我已根据IBM文章http://www-01.ibm.com/support/knowledgecenter/SS7K4U_7.0.0/com.ibm.websphere.web2mobile.mobile.application.services.help/docs/fileuploader_README.html?cp=SS7K4U_7.0.0%2F8-13-3为文件上传配置了REST通道:

import org.apache.wink.common.model.multipart.BufferedInMultiPart;

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@Path("/upload")
public RestResult upload(BufferedInMultiPart bimp) {
    List<InPart> parts = bimp.getParts();
    // ....
}

我已添加了流的测试消耗,只是为了确保请求已完全处理:

IOUtils.copy(part.getInputStream(), new NullOutputStream());

我已经用非常大的隆起测试了整体 - 大约100MB。我注意到Websphere服务器上显着的内存使用量增加,通过上传小文件大得多。我认为这是由于上传后和调用我的upload函数之前将上传的文件放入内存引起的。

是否可以配置工具以便将大文件读入某个临时文件而不是内存?

或者是否可以直接在REST channnel方法中成为传入输入流?

我正在使用Websphere 8.5和JAX-WS REST频道。

1 个答案:

答案 0 :(得分:2)

根据发现here的Apache Wink文档,似乎BufferedInMultiPart将完整文件存储到内存中,因此尝试用InMultiPart替换BufferedInMultiPart:

import org.apache.wink.common.model.multipart.InMultiPart;

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@Path("/upload")
public RestResult upload(InMultiPart bimp) {
    List<InPart> parts = new ArrayList<InPart>();
    while(bimp.hasNext()) {
        parts.add(bimp.next());
    }
    // ....
}