Improved temporary file removing.

This commit is contained in:
Christian P. MOMON 2022-02-20 02:16:47 +01:00
parent d017781858
commit 380028376a

View file

@ -105,9 +105,9 @@ public class Crawler
{ {
logger.info("Crawling {}", url); logger.info("Crawling {}", url);
File downloadFile = null;
try try
{ {
File downloadFile;
try try
{ {
downloadFile = download(url); downloadFile = download(url);
@ -158,6 +158,7 @@ public class Crawler
{ {
logger.error("ERROR: bad child class [{}][{}].", downloadClass, parent); logger.error("ERROR: bad child class [{}][{}].", downloadClass, parent);
this.journal.add(url, parentURL, CrawlStatus.BADCHILDCLASS); this.journal.add(url, parentURL, CrawlStatus.BADCHILDCLASS);
downloadFile.delete();
} }
else else
{ {
@ -177,6 +178,7 @@ public class Crawler
if (StringUtils.equals(downloadSha, storedSha)) if (StringUtils.equals(downloadSha, storedSha))
{ {
this.journal.add(url, parentURL, CrawlStatus.SUCCESS); this.journal.add(url, parentURL, CrawlStatus.SUCCESS);
downloadFile.delete();
} }
else else
{ {
@ -234,6 +236,13 @@ public class Crawler
{ {
this.journal.add(url, parentURL, CrawlStatus.IOERROR); this.journal.add(url, parentURL, CrawlStatus.IOERROR);
} }
finally
{
if ((downloadFile != null) && (downloadFile.exists()))
{
downloadFile.delete();
}
}
} }
/** /**
@ -325,6 +334,7 @@ public class Crawler
{ {
final int TIMEOUT = 5000; final int TIMEOUT = 5000;
result = Files.createTempFile("tmp-", ".statoolsinfos").toFile(); result = Files.createTempFile("tmp-", ".statoolsinfos").toFile();
result.deleteOnExit();
FileUtils.copyURLToFile(url, result, TIMEOUT, TIMEOUT); FileUtils.copyURLToFile(url, result, TIMEOUT, TIMEOUT);
} }