// 待处理的流

      

      // 定义文件根路径:TOMCAT的temp路径 + 时间戳

ByteArrayOutputStream bao = new ByteArrayOutputStream();

      

       //待输出文件流对象

String baseDir = System.getProperty("java.io.tmpdir")  + System.currentTimeMillis();

     

FileOutputStream fos = null;
        try {

             // 自定义文件路径

            

String customDir = "dir1/dir2/dir3";
             File file = new File(dirName + baseDir + "/" + customDir + "file.txt");

             //如果路径(无论多少层路径)不存在
           

if(!file.getParentFile().exists()){
                     file.getParentFile().mkdirs();
                     file.createNewFile();
             }
             fos = new FileOutputStream(file);
             bao.writeTo(fos);
             fos.flush();
             fos.close();
             bao.flush();
             bao.close();
        } catch (IOException ioe) {
              ioe.printStackTrace();
       }