getPath()与getAbsolutePath()的区别
public static void main(String[] args) { File file = new File("\\data\\draw_lottery\\test1.txt"); //取得相对路径 System.out.println("Path: " + file.getPath()); //取得绝对路径 window对应盘符的根路径 System.out.println("getAbsolutePath: " + file.getAbsolutePath()); }
结果:
Path: \data\draw_lottery\test1.txt
getAbsolutePath: D:\data\draw_lottery\test1.txt
图片上传到指定路径
String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath(); file.transferTo(Paths.get(absPath));
public static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException
{
File desc = new File(uploadDir + File.separator + fileName);
if (!desc.exists()){
if (!desc.getParentFile().exists()){
desc.getParentFile().mkdirs();
}
}
return desc;
}
关于transferTo,在本地没有指定盘符的情况下会报错
void transferTo(File dest) throws IOException, IllegalStateException; default void transferTo(Path dest) throws IOException, IllegalStateException { FileCopyUtils.copy(this.getInputStream(), Files.newOutputStream(dest)); }
浙公网安备 33010602011771号