java从数据库获取指定url路径,然后在本地新建

新手书写,仅供参考

package com.lk.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Test {
  public static void main(String[] args) throws IOException{
    FileInputStream in = null;
    FileOutputStream out =null;
    try {
      Class.forName("oracle.jdbc.driver.OracleDriver");
      Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.15.66:1521:orcl","ztq_fj","ztq_fj");
      Statement st=conn.createStatement();
      ResultSet rs=st.executeQuery("select path,replace(path,'d:/ftp/ztq_fj/yjxx_qxfxzh','d:/qxfw') path2 from test");
      int i=0;
      while(rs.next()) {
        String path2=rs.getString("path2");
        System.out.print((++i)+"\t"+path2+"\t");

        File filed = new File(path2);
        System.out.println("...."+filed.getName());

        if(!filed.exists()){
          if(filed.getName().indexOf(".") != -1){
            //文件
            filed.getParentFile().mkdirs();
            filed.createNewFile();
            //读取/写入文件
            FileInputStream input = new FileInputStream(filed);
            ByteArrayOutputStream bos =new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length = -1;
            while((length = input.read(buffer)) != -1){
            bos.write(buffer,0,length);
          }
          bos.close();
          input.close();
        }else{
          //文件夹
          filed.mkdirs();
        }
      }else{
        System.out.println("文件不存在!");
      }
    }
    rs.close();
    st.close();

    //关闭数据库
    conn.close();
    } catch (ClassNotFoundException | SQLException e) {
      // TODO 自动生成的 catch 块
      e.printStackTrace();
    }
  }
}

posted @ 2018-10-18 15:26  昊古  阅读(1495)  评论(0编辑  收藏  举报