Created
April 8, 2011 08:08
-
-
Save seyan/909468 to your computer and use it in GitHub Desktop.
ディレクトリトラバーサル対策:ファイル名にディレクトリ名が含まれないようにする
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
import static org.junit.Assert.assertEquals; | |
import java.io.File; | |
import org.junit.Test; | |
public class P237Test { | |
/** | |
* ファイルのパスを受け取り、ファイル名を返す。 | |
* ファイル名にディレクトリ名が含まれないようにする | |
* @param path | |
* @return | |
*/ | |
public String getFileBaseName(String path){ | |
if(path != null){ | |
File file = new File(path); | |
return file.getName(); | |
} | |
return null; | |
} | |
@Test | |
public void testGetBaseName() throws Exception{ | |
assertEquals(getFileBaseName("../../../test.txt"), "test.txt"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment