2016-11-21 105 views
0

我有位代碼,是應該在系統上的臨時文件的位置創建一個臨時目錄:NIO Files.createTempDirectory在Windows上引起異常

try { 
      Path tempdir = Files.createTempDirectory("tempmm"); 
      tempdir.toFile().deleteOnExit(); 
      tempFilename = format("%s/%s.zip", tempdir, meetId); 

      // Handle windows 
      //tempFilename = tempFilename.replace("\\", "/"); 

      uri = URI.create("jar:file:" + tempFilename); 

      System.out.println("temp file uri = " + uri.toString()); 

     } catch (IOException e) { 
      log.severe(format("Unable to create temporary directory: %s", e.toString())); 
    } 

try (FileSystem zipfs = FileSystems.newFileSystem(uri, new HashMap<String, String>() {{ put("create", "true"); }})) { 

      Path externalMMFile = Paths.get(filePath); 
      Path pathInZipfile = zipfs.getPath(externalMMFile.getFileName().toString()); 

      // copy Meet Manager Database file into the zip file 
      Files.copy(externalMMFile,pathInZipfile, StandardCopyOption.REPLACE_EXISTING); 
     } catch (IOException e) { 
      log.severe("Unable to create zip upload file!"); 
      System.out.println(e.toString()); 
      return false; 
} 

這是發生的異常:

java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException: Ill 
egal character in opaque part at index 11: jar:file:C:\DOCUME~1\David\LOCALS~1\T 
emp\tempmm6286934818003944424/107.zip 
java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException: Ill 
egal character in opaque part at index 11: jar:file:C:\DOCUME~1\David\LOCALS~1\T 
emp\tempmm6286934818003944424/107.zip 

有沒有人有任何想法我可以這樣做在AP獨立於平臺的方式?

回答

0

我發現了這個問題。一個URI是這樣的:

jar:file:C:/temp/yourmum.zip 

將無法​​工作在Windows上。它應該是:

jar:file:/C:/temp/yourmum.zip 

所以我這樣做:

try { 
     Path tempdir = Files.createTempDirectory("tempmm"); 
     tempdir.toFile().deleteOnExit(); 
     tempFilename = format("%s" + File.separator + "%s.zip", tempdir, meetId); 

     if (tempFilename.contains(":\\")) { 
      tempFilename = "/" + tempFilename; 
     } 

     // Handle windows 
     tempFilename = tempFilename.replace("\\", "/"); 

     System.out.println("tempFilename = " + tempFilename); 

     uri = URI.create("jar:file:" + tempFilename); 

     System.out.println("temp file uri = " + uri.toString()); 

    } catch (IOException e) { 
     log.severe(format("Unable to create temporary directory: %s", e.toString())); 
    } 

我不知道這是不是最好的解決辦法,但它爲我工作。 tild不是一個問題,它是分離器。