2016-08-09 10 views
1

當文件名包含「#」時,發現錯誤。當文件名包含帶「#」的文件名使用彈簧時找不到文件資源

錯誤:java.lang.IllegalStateException:文件不存在。

但是,當沒有「#」的文件名仍然工作。

String resource = "file:c:/Test#.txt"; 
PathMatchingResourcePatternResolver pathResolver = new PathMatchingResourcePatternResolver(); 
    Resource[] resolveResources; 
    try { 
     resolveResources = pathResolver.getResources(resources); 
     if(resolveResources.length == 0) { 
      throw new IllegalStateException("File does not exist: " + resources); 
     }else{ 
      for (Resource resource : resolveResources) { 
       if(!resource.exists()){ //true 
        throw new IllegalStateException("File does not exist: " + resource); 
       } 
      } 
     } 
    } catch (IOException e) { 
     throw new IllegalStateException("File does not exist: " + resources); 
    } 
+1

什麼會'resource.replace( 「#」, 「%23」)'做? '#''只在URL中表示爲錨點,而不是URI。試試'「file:/ c:/ ...」'。 –

+0

謝謝! Joop Eggen。它正在工作! – user3197661

+0

哪一個?我的猜測'「文件:/ c:/ ...」' –

回答

1

文件名的有效性取決於操作系統(文件系統更準確),所以,以確保該文件可以在任何操作系統上讀取;使用字母數字和在你的程序的下劃線Characters to avoid

的問題是在URI進行功能

public static void main(String[] args){ 


    File f = new File("ahh#.txt");  

try { 
    if(f.createNewFile()){ 
     System.out.println("ok"+ f.getAbsolutePath()); 
    } 
} catch (IOException ex) { 
    Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); 
} 
    // file with string path 
    FileInputStream fis0; 
    try { 
     fis0 = new FileInputStream(
       new File(f.getAbsolutePath())); 
    } catch (FileNotFoundException ex) { 
     System.out.println("file not found with string"); 
    } 

    try { 
     try { 
      // file with URI 
      FileInputStream fis1 = 
        new FileInputStream(
          new File(
            new URI(f.getAbsolutePath()))); 
     } catch (URISyntaxException ex) { 
      System.out.println("URI???"); 
     } 
    } catch (FileNotFoundException ex) { 
     System.out.println("file not found with URI"); 
    } 


} 
0

這是一個黑客位,但我認爲你需要像這樣:

@Test 
public void test1() { 

try { 
    String resource = "file:c:\\tmp\\Test\u0023.txt"; 

    PathMatchingResourcePatternResolver pathResolver = new TestPathMatchingResourcePatternResolver(); 
    Resource[] resolveResources; 
    resolveResources = pathResolver.getResources(resource); 
    if (resolveResources.length == 0) { 
     throw new IllegalStateException("File does not exist: " + resource); 
    } else { 
     for (Resource resource2 : resolveResources) { 
      if (!resource2.exists()) { // true 
       throw new IllegalStateException("File does not exist: " + resource2); 
      } 
     } 
    } 
} catch (Exception e) { 
    e.printStackTrace(); 
    fail(); 
} 

}

然後你需要添加修改PathMatchingResourcePatternResolver

public class TestPathMatchingResourcePatternResolver extends PathMatchingResourcePatternResolver { 

    public TestPathMatchingResourcePatternResolver() { 
     super(new TestDefaultResourceLoader()); 
    } 
} 

然後,你需要修改DefaultResourceLoader

public class TestDefaultResourceLoader extends DefaultResourceLoader { 
    @Override 
    public Resource getResource(String location) { 
     Assert.notNull(location, "Location must not be null"); 
     if (location.startsWith("/")) { 
      return getResourceByPath(location); 
     } else if (location.startsWith(CLASSPATH_URL_PREFIX)) { 
      return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader()); 
     } else { 
      try { 
       // Try to parse the location as a URL... 
       URL url = new URL(location); 
       return new TestUrlResource(url); 
      } catch (MalformedURLException ex) { 
       // No URL -> resolve as resource path. 
       return getResourceByPath(location); 
      } 
     } 
    } 
} 

然後,你需要修改UrlResource對象

public class TestUrlResource extends UrlResource { 

    public TestUrlResource(URL url) { 
     super(url); 
    } 

    @Override 
    public boolean exists() { 
     return true; 
    } 

    @Override 
    public File getFile() throws IOException { 
     return new File(getURL().toString().replace("file:", "")); 
    } 
} 
相關問題