Java sun jersey文件上载OutOfMemoryError

Java sun jersey文件上载OutOfMemoryError,java,file-upload,jersey,out-of-memory,Java,File Upload,Jersey,Out Of Memory,我有一个客户机代码,过去可以工作,但随着文件变大,我的内存不足错误。我尝试了FileDataBodyPart和StreamDataBodyPart,但都失败了,出现了相同的错误。它由多个部分组成,其中包含文件和json。感谢您的帮助 下面是代码 public static Import importFile(Log notify, String importId, File file) throws CheckedContentException { InputStream fileSt

我有一个客户机代码,过去可以工作,但随着文件变大,我的内存不足错误。我尝试了FileDataBodyPart和StreamDataBodyPart,但都失败了,出现了相同的错误。它由多个部分组成,其中包含文件和json。感谢您的帮助

下面是代码

public static Import importFile(Log notify, String importId, File file) throws CheckedContentException {
    InputStream fileStream = null;
    try {
        LOG.warn("Uploading file " + file.getName() + " with import id " + importId);
        JsonObject metadata = new JsonObject();
        metadata.addProperty("fileType", "xyz");
        metadata.add("fileOptions", new JsonObject());
        //
        // FileDataBodyPart filePart = new FileDataBodyPart(file.getName(), file);
        fileStream = Files.asByteSource(file).openStream();
        StreamDataBodyPart filePart = new StreamDataBodyPart("file", fileStream, file.getName());
        FormDataMultiPart multipart = new FormDataMultiPart();
        multipart.field("metadata", metadata.toString(), MediaType.APPLICATION_JSON_TYPE).bodyPart(filePart);
        //
        ClientResponse response = getNewWebResourceForImport(NEW_IMPORT_PATH, importId).type(MediaType.MULTIPART_FORM_DATA_TYPE)
                .accept(MediaType.APPLICATION_JSON_TYPE).post(ClientResponse.class, multipart);
        //
        String result = response.getEntity(String.class);
        Import imp = new Gson().fromJson(result, Import.class);
        if (response.getStatus() != 200 || StringUtils.equals(imp.getStatus(), IMPORT_FILE_UPLOAD_ERROR)) {
            throw new CheckedContentException("Failed to Import File with status " + response.getStatus() + " : " + response);
        }
        notify.warn("Finished uploading File " + file.getName() + " with id " + importId);
        return imp;
    } catch (CheckedContentException e) {
        throw e;
    } catch (Exception e) {
        throw new CheckedContentException(e);
    } finally {
        IOUtils.closeQuietly(fileStream);
    }
}
private static Builder getNewWebResourceForImport(String path, String importId) {
    Client client = Client.create();
    client.addFilter(new LoggingFilter(System.out));
    WebResource webResource = client.resource(WS_URL).path(path).path(importId).path("files");
    return webResource.header(ACCESS_TOKEN_KEY, API_ACCESS_TOKEN).header(CLIENT_SECRET_TOKEN_KEY, CLIENT_TOEKN);
}
java.lang.OutOfMemoryError
at java.io.ByteArrayOutputStream.hugeCapacity(Unknown Source)
at java.io.ByteArrayOutputStream.grow(Unknown Source)
at java.io.ByteArrayOutputStream.ensureCapacity(Unknown Source)
at java.io.ByteArrayOutputStream.write(Unknown Source)
at com.sun.jersey.api.client.filter.LoggingFilter$LoggingOutputStream.write(LoggingFilter.java:109)
at com.sun.jersey.core.util.ReaderWriter.writeTo(ReaderWriter.java:115)
at com.sun.jersey.core.provider.AbstractMessageReaderWriterProvider.writeTo(AbstractMessageReaderWriterProvider.java:76)
at com.sun.jersey.core.impl.provider.entity.InputStreamProvider.writeTo(InputStreamProvider.java:98)
at com.sun.jersey.core.impl.provider.entity.InputStreamProvider.writeTo(InputStreamProvider.java:59)
at com.sun.jersey.multipart.impl.MultiPartWriter.writeTo(MultiPartWriter.java:220)
at com.sun.jersey.multipart.impl.MultiPartWriter.writeTo(MultiPartWriter.java:73)
at com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:300)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:217)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:153)
at com.sun.jersey.api.client.filter.LoggingFilter.handle(LoggingFilter.java:217)
at com.sun.jersey.api.client.Client.handle(Client.java:652)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:570)

解决方案非常简单,我需要添加ClientConfig.PROPERTY\u CHUNKED\u ENCODING\u SIZE并设置它

client.setChunkedEncodingSize(32 * 1024);
它没有工作,直到我删除了日志文件管理器,因为它正在耗尽内存,我在第一个地方添加了它进行调试,但忘记了它。在研究分块的大小时,我偶然发现这篇文章也有同样的问题,并以同样的方式解决了它。希望我在挣扎之前找到它,希望这对我有所帮助。