关闭传递给MultipartFormDataOutput的流

huangapple go评论96阅读模式
英文:

Closing Streams passed to MultipartFormDataOutput

问题

Resteasy文档没有解释谁负责关闭传递给MultipartFormDataOutput的流。让我们考虑以下示例:

WebTarget target = ClientBuilder.newClient().target("url");
MultipartFormDataOutput formData = new MultipartFormDataOutput();
FileInputStream fis1 = new FileInputStream(new File("/path/to/image1"));
FileInputStream fis2 = new FileInputStream(new File("/path/to/image2"));
formData.addFormData("image", fis1, MediaType.APPLICATION_OCTET_STREAM_TYPE);
formData.addFormData("image", fis2, MediaType.APPLICATION_OCTET_STREAM_TYPE);
Entity<MultipartFormDataOutput> entity = Entity.entity(formData, MediaType.MULTIPART_FORM_DATA);
Response response = target.request().post(entity);

fis1fis2会由Resteasy关闭,还是用户应该注意关闭这些流?

英文:

Resteasy docs does not explain who is responsible for closing streams passed to MultipartFormDataOutput. Let's consider the following example:

WebTarget target = ClientBuilder.newClient().target(&quot;url&quot;);
MultipartFormDataOutput formData = new MultipartFormDataOutput();
FileInputStream fis1 = new FileInputStream(new File(&quot;/path/to/image1&quot;));
FileInputStream fis2 = new FileInputStream(new File(&quot;/path/to/image2&quot;));
formData.addFormData(&quot;image&quot;, fis1, MediaType.APPLICATION_OCTET_STREAM_TYPE);
formData.addFormData(&quot;image&quot;, fis2, MediaType.APPLICATION_OCTET_STREAM_TYPE);
Entity&lt;MultipartFormDataOutput&gt; entity = Entity.entity(formData, MediaType.MULTIPART_FORM_DATA);
Response response = target.request().post(entity);

Will the fis1 and fis2 be closed by the resteasy or the user should take care of closing these streams?

答案1

得分: 0

我建议使用try-with-resource确保它们会被关闭。

WebTarget target = ClientBuilder.newClient().target("url");
MultipartFormDataOutput formData = new MultipartFormDataOutput();

try(FileInputStream fis1 = new FileInputStream(new File("/path/to/image1"));
    FileInputStream fis2 = new FileInputStream(new File("/path/to/image2")))
{
  formData.addFormData("image", fis1, MediaType.APPLICATION_OCTET_STREAM_TYPE);
  formData.addFormData("image", fis2, MediaType.APPLICATION_OCTET_STREAM_TYPE);
  Entity<MultipartFormDataOutput> entity = Entity.entity(formData, 
  MediaType.MULTIPART_FORM_DATA);
  Response response = target.request().post(entity);
}
英文:

I would suggest to use try-with-resource to be sure that they will be closed.

WebTarget target = ClientBuilder.newClient().target(&quot;url&quot;);
MultipartFormDataOutput formData = new MultipartFormDataOutput();

try(FileInputStream fis1 = new FileInputStream(new File(&quot;/path/to/image1&quot;)));
    FileInputStream fis2 = new FileInputStream(new File(&quot;/path/to/image2&quot;)))
{
  formData.addFormData(&quot;image&quot;, fis1, MediaType.APPLICATION_OCTET_STREAM_TYPE);
  formData.addFormData(&quot;image&quot;, fis2, MediaType.APPLICATION_OCTET_STREAM_TYPE);
  Entity&lt;MultipartFormDataOutput&gt; entity = Entity.entity(formData, 
  MediaType.MULTIPART_FORM_DATA);
  Response response = target.request().post(entity);
}

答案2

得分: 0

为了能够自己回答我的问题,希望有人能从中受益。

Resteasy将关闭传递的流。在我的情况下,InputStreamProvider将负责关闭FileInputStream

public void writeTo(InputStream inputStream, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException
{
    LogMessages.LOGGER.debugf("Provider: %s, Method: writeTo", getClass().getName());
    try
    {
        int c = inputStream.read();
        if (c == -1)
        {
            httpHeaders.putSingle(HttpHeaderNames.CONTENT_LENGTH, Integer.toString(0));
            entityStream.write(new byte[0]); // fix RESTEASY-204
            return;
        }
        else
            entityStream.write(c);
        ProviderHelper.writeTo(inputStream, entityStream);
    }
    finally
    {
        inputStream.close();
    }
}
英文:

So I can answer my question myself, hope someone will benefit from this.

Resteasy will close the passed stream. In my case, the InputStreamProvider will take care of closing the FileInputStream.

     public void writeTo(InputStream inputStream, Class&lt;?&gt; type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap&lt;String, Object&gt; httpHeaders, OutputStream entityStream) throws IOException
   {
           LogMessages.LOGGER.debugf(&quot;Provider : %s,  Method : writeTo&quot;, getClass().getName());
	   try
	   {
		   int c = inputStream.read();
		   if (c == -1)
		   {
			   httpHeaders.putSingle(HttpHeaderNames.CONTENT_LENGTH, Integer.toString(0));
			   entityStream.write(new byte[0]); // fix RESTEASY-204
			   return;
		   }
		   else
			   entityStream.write(c);
         ProviderHelper.writeTo(inputStream, entityStream);
	   }
	   finally
	   {
         inputStream.close();
	   }
   }

huangapple
  • 本文由 发表于 2020年9月29日 15:30:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64114835.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定