我有个程序,要调用个存储过程,存储过程执行(大约要40分钟)结束将产生几个文件,文件是由存储过程产生的,然后通过程序来下载这几个文件,但是我在执行程序的时候,发生了这种情况:文件还没产生或者说是存储过程还没完全执行结束,程序就开始执行接下来的代码了,这样就导致了我不能下载到这几个文件。我想请教发生这种问题的原因和解决这个问题的办法。我试过把接下来的代码sleep一定的时间,但是还是程序还是不能正确执行。程序的部分代码是:
public static void main(String[] args) {
FtpUtils fu = new FtpUtils(); //包含ftp下载或者上传的常用方法。
JdbcUtils ju = new JdbcUtils();//数据库连接 ,关闭,执行存储过程的常用方法
ju.doProcedure(ArgumentInit.procname,
ArgumentInit.dburl, ArgumentInit.dbuser,
ArgumentInit.dbpassword, ArgumentInit.driveclass); fu.downLoadFile(ArgumentInit.dbftpIP, ArgumentInit.dbftpport,
ArgumentInit.dbftpusername, ArgumentInit.dbftppassword,
ArgumentInit.etllocalpath, ArgumentInit.dbftpremotepath, true,
ArgumentInit.file1regex);
//JdbcUtils类中执行存储过程的方法
public void doProcedure(String procname, String url,
String user, String password, String driveclass) { Connection conn = this.getConn(url, user, password, driveclass);
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(procname);
ps.execute();
} catch (SQLException e) {
System.out.println("Procedure exception");
e.printStackTrace();
}
this.freeDBRes(conn, ps);
}
//FtpUtils类中下载文件的方法
public void downLoadFile(String IP, int port, String username,
String password, String localpath, String remotepath, boolean flag,
String regex) { FTPClient ftp = new FTPClient();
try {
ftp.setRemoteHost(IP);
ftp.setRemotePort(port);
ftp.setControlEncoding("gb2312"); //确定中文编码方式
ftp.connect(); //连接到远程FTP服务器上
ftp.setTimeout(30000);
ftp.login(username, password); //登陆远程ftp服务器
ftp.setType(FTPTransferType.BINARY); //传输模式为二进制
ftp.setConnectMode(FTPConnectMode.PASV);
ftp.chdir(remotepath); //进入到所需数据的文件夹下
String[] filename = ftp.dir();
if (flag == true) { //需要过滤文件件名
for (int i = 0; i < filename.length; i++) { //遍历文件夹下所有文件
if (fou.findFile(regex, filename[i])) {
System.out.println(fou.findFile(regex, filename[i]));
//按照regstr给出的字符串下载需要的
System.out.println(localpath + filename[i]);
String remotefile = filename[i];
ftp.get(localpath + remotefile, remotefile); //下载远程文件到本地
//ftp.delete(remotefile); //删除远程文件
}
} } else { //不需要过滤文件,即把该目录下面的所有文件都down下来。
for (int i = 0; i < filename.length; i++) {
if (fou.findFile(regex, filename[i])) {
ftp.get(localpath + filename[i], filename[i]);
}
}
} ftp.quit();
} catch (IOException e) {
e.printStackTrace();
System.out.println("Ftp connect Exception");
} catch (FTPException e) {
e.printStackTrace();
System.out.println("Ftp connect Exception");
} }程序很乱,请大家多见谅。

解决方案 »

  1.   

    代码太乱,还没有看完
    给你个建议
    JdbcUtils ju = new JdbcUtils();//数据库连接 ,关闭,执行存储过程的常用方法 
    这个数据库获得连接的类,你还是不要new吧,用静态的获取,因为数据库连接一般用单例,你开得多了可能会有问题
      

  2.   

    楼主可以给存储过程返回一个值,如下:while(true){
       //execute procedure
    }
    //ftp
      

  3.   

    在下载的代码段加上
    boolean flag=false;文件是否存在
    while(1==1){
        flag=//可以判断生成的几个文件是否存在 具体代码请参照file类
        if(flag)
        {
          //下载文件
           break;
        }
    }