Commit 9b9f3af2bab175ff11a96a9b663e1548d2abb903

Authored by Nathanael Jourdane
1 parent 2bd5d29b
Exists in master and in 1 other branch b6.0.X

Use File.createTempFile instead of manually create the file name.

Showing 1 changed file with 15 additions and 10 deletions   Show diff stats
src/main/java/eu/omp/irap/vespa/votable/utils/Network.java
... ... @@ -17,6 +17,7 @@
17 17 package eu.omp.irap.vespa.votable.utils;
18 18  
19 19 import java.io.BufferedReader;
  20 +import java.io.File;
20 21 import java.io.FileNotFoundException;
21 22 import java.io.IOException;
22 23 import java.io.InputStreamReader;
... ... @@ -25,8 +26,6 @@ import java.io.UnsupportedEncodingException;
25 26 import java.net.HttpURLConnection;
26 27 import java.net.URL;
27 28 import java.net.URLEncoder;
28   -import java.text.SimpleDateFormat;
29   -import java.util.Date;
30 29 import java.util.Map;
31 30 import java.util.logging.Level;
32 31 import java.util.logging.Logger;
... ... @@ -83,17 +82,23 @@ public class Network {
83 82 * @throws CantSendQueryException The query can not be sent.
84 83 */
85 84 public static String saveQuery(String url) throws CantSendQueryException {
86   - String dateStr = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss_SSS").format(new Date());
87   - String outputPath = Consts.TMP_DIR + "/epnTapLib_" + dateStr + ".xml";
  85 + File lOutTmpFile = null;
  86 + try {
  87 + lOutTmpFile = File.createTempFile("epnTapLib_", ".xml");
  88 + StringBuilder result = Network.sendGet(url);
88 89  
89   - StringBuilder result = Network.sendGet(url);
  90 + try (PrintWriter writer = new PrintWriter(lOutTmpFile, Consts.ENCODING)) {
  91 + writer.println(result);
  92 + } catch (FileNotFoundException | UnsupportedEncodingException e) {
  93 + throw new CantSendQueryException(
  94 + "Can not save query on the file " + lOutTmpFile.getAbsolutePath(), e);
  95 + }
90 96  
91   - try (PrintWriter writer = new PrintWriter(outputPath, Consts.ENCODING)) {
92   - writer.println(result);
93   - } catch (FileNotFoundException | UnsupportedEncodingException e) {
94   - throw new CantSendQueryException("Can not save query on the file " + outputPath, e);
  97 + } catch (IOException e) {
  98 + throw new CantSendQueryException("Cannot create temp file: " + e.getMessage(), e);
95 99 }
96   - return outputPath;
  100 +
  101 + return lOutTmpFile.getAbsolutePath();
97 102 }
98 103  
99 104 /**
... ...