在java裏怎麼處理文件呢?下面yjbys介紹一些利用JDK7標準庫來靈活處理文件的方法。希望對大家有幫助!
實用的工具類,Path,Paths,Files,FileSystem
有一些很靈活的處理方法:
//得到一個Path對象
Path path = ("/test/");
//Path轉換File
File file = le();
AllBytes(path);
teIfExists(path);
(path);
正確拼接路徑不要手動拼接路徑
不好的代碼:
String game = "foo";
File file = new File("~/test/" + game + "");
即使是要手動拼接路徑,請使用下面兩個平台無關的變量:
tln(Separator);
tln(rator);
正確簡潔的方法是使用Paths類:
Path path = ("~/test/", "foo", "bar", "");
tln(path);
// ~/test/foo/bar/
讀取文件的所有內容,文件的所有行
讀取文件所有內容前,先判斷文件大小,防止OOM。
public static byte[] readAllBytes(String fileName, long maxSize) throws IOException {
Path path = (fileName);
long size = (path);
if (size > maxSize) {
throw new IOException("file: " + path + ", size:" + size + "> " + maxSize);
}
return AllBytes(path);
}
public static List readAlllines(String fileName, Charset charset, long maxSize) throws IOException {
Path path = (fileName);
long size = (path);
if (size > maxSize) {
throw new IOException("file: " + path + ", size:" + size + "> " + maxSize);
}
return AllLines(path, charset);
}
利用JDK7的特性,auto close,遠離一堆的catch, close
Path path = ("~/test/", "foo", "bar", "");
try (InputStream in = nputStream(path)) {
// process
//();
}
歷遍目錄
DK7新特性,FileVisitor
public class MyFileVisitor extends SimpleFileVisitor{
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
tln(file);
return INUE;
}
public static void main(String[] args) throws IOException {
Path path = ("/home/user/test");
FileTree(path, new MyFileVisitor());
}
}
判斷文件是否在父路徑下
網上流傳一種遞歸判斷parent的方式,
但是查閲jdk代碼後,發現getParent()函數是通過處理文件名得到的。所以直接比較文件前綴即可。
請務必注意,anonicalPath()函數 。
public static boolean isSubFile(File parent, File child) throws IOException {
return anonicalPath()tsWith(anonicalPath());
}
public static boolean isSubFile(String parent, String child) throws IOException {
return isSubFile(new File(parent), new File(child));
}
監視文件改變
JDK7新特性,但是API比較難用。TODO
淘寶有個diamond的配置管理項目,是利用定時器不斷去讀取來文件是否改變的。
JDK7則是利用了linux的inotify機制。
Web服務器防止非法的文件路徑訪問
字符截斷攻擊和文件歷遍漏洞原理:在文件名中插入%00的URL編碼,web服務器會把%00後面的內容拋棄。
例如這樣的URL:http://www.test.com/../../../../etc/passwd%00.gif
防範方法
寫入文件前,判斷文件是否在父路徑下,參考上面的函數。
利用Java的安全機制
// All files in /img/java can be read
grant codeBase "file:/home/programpath/" {
permission Permission "/img/java", "read";
};
Tomcat的設置
靜態資源不要自己手寫代碼去讀取,儘量使用Web服務器或者Web框架的`本身的靜態資源映射功能。
比如Tomcat的默認自帶的DefaultServlet:
default
/static/*
Spring mvc可以配置
或者使用spring mvc裏的DefaultServletHttpRequestHandler。這個默認優先級是最低的,也就是最後沒人處理的URL會交給WebServer本身的default servlet去處理。比如Tomcat的就是上面所説的。
個人推薦使用DefaultServletHttpRequestHandler,因為Web容器的文件訪問功能要比Spring mvc自身的要強大。比如Tomcat的DefaultServlet支持Etag,斷點續傳,緩存等。
參考: