在一个SpringWeb应用程序中(虽然我不认为这与Spring有关),我创建了一个REST GET资源,允许下载当前的log4j2文件内容,但是请求(来自浏览器和cUrl)不会随着部署在远程生产服务器上的webapp而终止,而它们通常会在我的Windows开发机器上结束。
而且,cUrl说还有字节要接收:
* transfer closed with 3 bytes remaining to read
* stopped the pause stream!
* Closing connection 0
curl: (18) transfer closed with 3 bytes remaining to read
这是相关代码:
// ...
import org.springframework.http.HttpHeaders;
// ...
@RestController
@RequestMapping(path="/logs", produces="application/json")
public class LogController {
// ...
@GetMapping(path="/{appenderName}/contents", produces="text/plain")
public void download(@PathVariable String appenderName, HttpServletResponse response) {
// ...
org.apache.logging.log4j.Logger rootLogger = LogManager.getRootLogger();
if(rootLogger instanceof Logger) {
Logger l = (Logger) rootLogger;
if(l.getAppenders().containsKey(appenderName)) {
Appender appender = l.getAppenders().get(appenderName);
if(appender instanceof FileAppender) {
((FileAppender) appender).getManager().flush();
final File f = new File(((FileAppender) appender).getFileName());
response.setHeader(HttpHeaders.CONTENT_TYPE, "text/plain");
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + f.getName() + "\"");
Path tempCopy = Files.createTempFile("log-", null);
Files.copy(f.toPath(), tempCopy, StandardCopyOption.REPLACE_EXISTING);
response.setHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(tempCopy.toFile().length()));
Reader reader = new FileReader(tempCopy.toFile());
IOUtils.copy(reader, response.getWriter());
reader.close();
tempCopy.toFile().delete();
// ...
Content-length
header:在Linux(Ubuntu)服务器上,只有减去
3
文件长度:
response.setHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(tempCopy.toFile().length() - 3));
当然,如果我不寄,一切都好
Content-Length
标题。