-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6eadd01
commit 8079272
Showing
5 changed files
with
167 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 0 additions & 36 deletions
36
modules/utils/src/main/java/org/springside/modules/utils/io/GeneralResourceUtil.java
This file was deleted.
Oops, something went wrong.
80 changes: 80 additions & 0 deletions
80
modules/utils/src/main/java/org/springside/modules/utils/io/URLResourceUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package org.springside.modules.utils.io; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.MalformedURLException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.commons.lang3.Validate; | ||
|
||
/** | ||
* 兼容url为无前缀,file://与classpath:// 三种情况的工具集 | ||
* | ||
* 参考Spring ResourceUtils | ||
* | ||
* @author calvin | ||
*/ | ||
public class URLResourceUtil { | ||
|
||
private static final String CLASSPATH_PREFIX = "classpath://"; | ||
|
||
private static final String URL_PROTOCOL_FILE = "file"; | ||
|
||
/** | ||
* 兼容无前缀, classpath://, file:// 的情况获取文件 | ||
*/ | ||
public static File asFile(String generalPath) throws IOException { | ||
if (StringUtils.startsWith(generalPath, CLASSPATH_PREFIX)) { | ||
String resourceName = StringUtils.substringAfter(generalPath, CLASSPATH_PREFIX); | ||
return getFileByURL(ResourceUtil.asUrl(resourceName)); | ||
} | ||
try { | ||
// try URL | ||
return getFileByURL(new URL(generalPath)); | ||
} catch (MalformedURLException ex) { | ||
// no URL -> treat as file path | ||
return new File(generalPath); | ||
} | ||
} | ||
|
||
/** | ||
* 兼容file://与classpath://的情况的打开文件成Stream | ||
*/ | ||
public static InputStream asStream(String generalPath) throws IOException { | ||
if (StringUtils.startsWith(generalPath, CLASSPATH_PREFIX)) { | ||
String resourceName = StringUtils.substringAfter(generalPath, CLASSPATH_PREFIX); | ||
return ResourceUtil.asStream(resourceName); | ||
} | ||
|
||
try { | ||
// try URL | ||
return FileUtil.asInputStream(getFileByURL(new URL(generalPath))); | ||
} catch (MalformedURLException ex) { | ||
// no URL -> treat as file path | ||
return FileUtil.asInputStream(generalPath); | ||
} | ||
} | ||
|
||
private static File getFileByURL(URL fileUrl) throws FileNotFoundException { | ||
Validate.notNull(fileUrl, "Resource URL must not be null"); | ||
if (!URL_PROTOCOL_FILE.equals(fileUrl.getProtocol())) { | ||
throw new FileNotFoundException("URL cannot be resolved to absolute file path " | ||
+ "because it does not reside in the file system: " + fileUrl); | ||
} | ||
try { | ||
return new File(toURI(fileUrl.toString()).getSchemeSpecificPart()); | ||
} catch (URISyntaxException ex) { | ||
// Fallback for URLs that are not valid URIs (should hardly ever happen). | ||
return new File(fileUrl.getFile()); | ||
} | ||
} | ||
|
||
public static URI toURI(String location) throws URISyntaxException { | ||
return new URI(StringUtils.replace(location, " ", "%20")); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
modules/utils/src/test/java/org/springside/modules/utils/io/URLResourceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.springside.modules.utils.io; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import org.junit.Test; | ||
|
||
public class URLResourceTest { | ||
|
||
@Test | ||
public void resource() throws IOException { | ||
File file = URLResourceUtil.asFile("classpath://application.properties"); | ||
assertThat(FileUtil.toString(file)).isEqualTo("springside.min=1\nspringside.max=10"); | ||
|
||
InputStream is = URLResourceUtil.asStream("classpath://application.properties"); | ||
assertThat(IOUtil.toString(is)).isEqualTo("springside.min=1\nspringside.max=10"); | ||
IOUtil.closeQuietly(is); | ||
|
||
try { | ||
URLResourceUtil.asFile("classpath://notexist.properties"); | ||
fail("should fail"); | ||
} catch (Throwable t) { | ||
assertThat(t).isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
try { | ||
URLResourceUtil.asStream("classpath://notexist.properties"); | ||
fail("should fail"); | ||
} catch (Throwable t) { | ||
assertThat(t).isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
} | ||
|
||
@Test | ||
public void file() throws IOException { | ||
File file = FileUtil.createTempFile(); | ||
FileUtil.write("haha", file); | ||
try { | ||
File file2 = URLResourceUtil.asFile("file://" + file.getAbsolutePath()); | ||
assertThat(FileUtil.toString(file2)).isEqualTo("haha"); | ||
|
||
try { | ||
URLResourceUtil.asFile("file://" + file.getAbsolutePath() + ".noexist"); | ||
fail("should fail"); | ||
} catch (Throwable t) { | ||
assertThat(t).isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
File file3 = URLResourceUtil.asFile(file.getAbsolutePath()); | ||
assertThat(FileUtil.toString(file3)).isEqualTo("haha"); | ||
try { | ||
URLResourceUtil.asFile(file.getAbsolutePath() + ".noexist"); | ||
fail("should fail"); | ||
} catch (Throwable t) { | ||
assertThat(t).isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
} finally { | ||
FileUtil.deleteFile(file); | ||
} | ||
|
||
} | ||
|
||
} |