我正在使用 org.apache.commons.httpclient.methods.PostMethod 类的 getResponseBody() 方法。 但是,我总是收到在运行时写入控制台的消息:
警告:要缓冲较大或未知大小的响应正文。 建议使用 getResponseBodyAsStream。
在代码中,无论如何我都必须将响应写入字节数组,因此我应该使用 getResponseBody() 方法。 但是有没有一种简单的方法可以抑制警告消息,这样我就不必在每次运行时都查看它?
如果是编译器错误,我会使用 @SuppressWarnings 注释,但这不是编译时问题; 它发生在运行时。 另外,我可以使用 getResponseBodyAsStream 写入 ByteArrayOutputStream,但这似乎是一种绕过警告的狡猾方法(额外的代码行来完成 getResponseBody() 已经为我做的事情)。
我的猜测是答案涉及 System.out 或 System.err 操作,但是有没有好的方法可以做到这一点?
I am using the getResponseBody() method of the org.apache.commons.httpclient.methods.PostMethod class. However, I am always getting a message written to the console at runtime:
WARNING: Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
In the code I have to write the response to a byte array anyway, so it is the getResponseBody() method that I ought to use. But is there an easy way that I can suppress the warning message so I don't have to look at it at every run?
If it was a compiler error, I'd use the @SuppressWarnings annotation, but this isn't a compile-time issue; it happens at runtime. Also, I could use getResponseBodyAsStream to write to a ByteArrayOutputStream, but this seems like a hacky way to get around the warning (extra lines of code to do what getResponseBody() is already doing for me).
My guess is that the answer involves System.out or System.err manipulation, but is there a good way to do this?
发布评论
评论(7)
简单地“保存”stderr 消息,然后在完成主要任务后打印它们
to simply 'save' the stderr messages then print them after completion of the main task
如果您想彻底停止此日志条目,可以使用一个可调的最大值来触发警告。 我看到了这个查看代码。
HttpMethodBase.setParams() 看起来像是将 HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT 设置为所需值的地方。
If you want to cleanly stop this log entry, there's a tunable max that triggers the warning. I saw this looking at the code.
HttpMethodBase.setParams() looks like the place to set HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT to the desired value.
当 httpClient 不知道返回数据的长度时会发生该警告
您应该在服务器端设置内容长度属性,该警告应该消失
之后
that warning happends when httpClient have no idea about the length of the return data
you should set the content-length attribute in your server end
after that, that warning should disapear
我建议您按照警告的建议进行操作,并使用流而不是字节数组。 如果您尝试推送的响应特别大(假设它是一个大文件),您会将其全部加载到内存中,这将是一件非常糟糕的事情。
使用流确实会更好。
也就是说,您可以通过临时替换 System.err 或 System.out 来解决它。 它们只是
PrintStream
对象,并且可以使用 setOut 和 setErr 方法进行设置。编辑:
如果您可以修改 API,请这样做。 在这种情况下,流处理是最好的方法。 如果由于内部压力或其他原因而不能,请转到 @John M 的路由并增加
BUFFER_WARN_TRIGGER_LIMIT
- 但请确保您有一个已知的 contentLength,否则该路由甚至会失败。I would recommend that you do as the warning suggests and use a stream rather than a byte array. If the response you're trying to push is particularly large (suppose it's a large file), you will load it all into memory, and that'd be a very bad thing.
You're really better off using streams.
That said, you might hack around it by replacing System.err or System.out temporarily. They're just
PrintStream
objects, and they're settable with the setOut and setErr methods.Edit:
If you can modify the API, do so. Stream processing is the best way to go in this case. If you can't due to internal pressures or whatever, go @John M's route and pump up the
BUFFER_WARN_TRIGGER_LIMIT
-- but make sure you have a known contentLength, or even that route will fail.库是通过log4j输出的吗? 如果是这样,编辑 log4j.properties 将此类的输出设置为“ERROR”将有效,例如
Is the library outputting through log4j? if so, editing the log4j.properties to set the output for this class to "ERROR" would work, e.g.
此问题已在 ASF JIRA 上进行过讨论。 有两种方法可以解决此问题:
This issue has already been debated on the ASF JIRA. There are two ways to resolve this:
假设警告已写入 stderr,您始终可以通过将 stderr 管道传输到
/dev/null
或系统上的任何等效项来抑制警告。Assuming the warning is written to stderr, you can always suppress the warning by piping stderr to
/dev/null
, or whatever the equivalent on your system is.