2016-11-21 1 views
0

ich ein Stück Code haben, der ein temporäres Verzeichnis in dem temporären Speicherort der Datei auf dem System erstellen angenommen hat:NIO Files.createTempDirectory auf Windows verursacht Ausnahme

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())); 
    } 

und

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; 
} 

Dies ist die Ausnahme, die auftritt:

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 

Hat jemand eine Idee, wie ich dies in ap tun kann latform-unabhängige Weise?

Antwort

0

Ich erkannte das Problem. Ein URI wie folgt:

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

funktioniert nicht unter Windows. Es sollte sein:

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

Also tat ich dies:

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())); 
    } 

Ich weiß nicht, ob dies die beste Lösung ist, aber es funktioniert für mich. Die Tilds waren nicht alles ein Problem, es waren die Separatoren.

Verwandte Themen