File.separator 根据wind linux地址 //本地上传 复制 public static void main(String[] args) throws IOException { File file = new File("D:\\Test\\test.txt"); String originalPath="D:\\Test"; SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd"); String extension = file.getAbsolutePath().substring(file.getAbsolutePath().indexOf(".")); // String extension = file.substring(file.lastIndexOf(".") + 1); Date date = new Date(); String upLoadAbsolutePath = originalPath + dateformat.format(date) + File.separator + file.getName() + "." + extension; //创建目录结构 createParentDir(upLoadAbsolutePath); //文件复制 FileUtils.copyInputStreamToFile(new FileInputStream(file), new File(upLoadAbsolutePath)); }
/** * 创建目录结构 * * @param fileName */ public static void createParentDir(String fileName) { if (null != fileName && fileName.trim().length() > 0) { File file = new File(fileName); String parent = file.getParent(); File tfdir = new File(parent); if (!tfdir.exists() || !tfdir.isDirectory()) { tfdir.mkdirs(); } tfdir.setReadable(true, false); tfdir.setWritable(true, false); file.setReadable(true, false);//设置可读权限 file.setWritable(true, false);//设置可写权限 } }