【问题标题】:sun jersey File upload OutOfMemoryError太阳球衣文件上传OutOfMemoryError
【发布时间】:2019-02-04 14:40:52
【问题描述】:

我有一个过去可以工作的客户端代码,但随着文件变大,出现内存不足错误。我尝试了 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)

【问题讨论】:

    标签: java file-upload jersey out-of-memory


    【解决方案1】:

    解决方案很简单,我需要添加ClientConfig.PROPERTY_CHUNKED_ENCODING_SIZE并设置。

    client.setChunkedEncodingSize(32 * 1024);
    

    直到我删除 Logging filer 因为内存不足,它才起作用,我首先添加它进行调试但忘记了它。在研究为分块设置什么大小时,我偶然发现了这篇有同样问题的文章,并以同样的方式解决了它。希望我在斗争之前找到它,希望这会有所帮助。 http://davidbuccola.blogspot.com/2009/09/configure-jersey-chunked-encoding-when.html

    【讨论】:

      猜你喜欢
      • 2014-07-18
      • 1970-01-01
      • 2012-05-22
      • 2014-02-02
      • 1970-01-01
      • 2019-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多