Skip to content

Commit

Permalink
#525 URLResouceUtil加强
Browse files Browse the repository at this point in the history
  • Loading branch information
calvin1978 committed Jan 24, 2017
1 parent 6eadd01 commit 8079272
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springside.modules.utils.io.GeneralResourceUtil;
import org.springside.modules.utils.io.IOUtil;
import org.springside.modules.utils.io.URLResourceUtil;
import org.springside.modules.utils.number.NumberUtil;

/**
Expand Down Expand Up @@ -51,13 +51,13 @@ public static String getString(Properties p, String name, String defaultValue) {
/**
* 从文件路径加载properties.
*
* 路径支持从外部文件或resources文件加载, "file://"代表外部文件, "classpath://"代表resources,
* 路径支持从外部文件或resources文件加载, "file://"或无前缀代表外部文件, "classpath://"代表resources,
*/
public static Properties loadFromFile(String generalPath) {
Properties p = new Properties();
InputStream is = null;
try {
is = GeneralResourceUtil.asStream(generalPath);
is = URLResourceUtil.asStream(generalPath);
p.load(is);
} catch (IOException e) {
logger.warn("Load property from " + generalPath + " fail ", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
* @author calvin
*/
public class FileUtil {



//////// 文件读写//////

Expand Down Expand Up @@ -77,13 +79,27 @@ public static void append(final CharSequence from, final File to) throws IOExcep
public static InputStream asInputStream(String fileName) throws IOException {
return new FileInputStream(getFileByPath(fileName));
}

/**
* 打开文件为InputStream
*/
public static InputStream asInputStream(File file) throws IOException {
return new FileInputStream(file);
}

/**
* 打开文件为OutputStream
*/
public static OutputStream asOututStream(String fileName) throws IOException {
return new FileOutputStream(getFileByPath(fileName));
}

/**
* 打开文件为OutputStream
*/
public static OutputStream asOututStream(File file) throws IOException {
return new FileOutputStream(file);
}

/**
* 获取File的BufferedReader
Expand Down

This file was deleted.

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

}

}

0 comments on commit 8079272

Please sign in to comment.