Skip to content

Instantly share code, notes, and snippets.

@seyan
Created April 8, 2011 08:08
Show Gist options
  • Save seyan/909468 to your computer and use it in GitHub Desktop.
Save seyan/909468 to your computer and use it in GitHub Desktop.
ディレクトリトラバーサル対策:ファイル名にディレクトリ名が含まれないようにする
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