1、我用的是commons.net-2.0的jar包。
ftpClient有一种方法是 ftpClient.getModificationTime(String pathname)可以得到文件的最后修改时间。
但是我用这种方法总是抛出异常“Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 33”
这是为什么啊?String time = ftpClient.getModificationTime("C:/CLIENT/001.0A1");
System.out.println(time);2、情景:远程ftp服务器会更新某目录下的一些文件,本地服务器需要定时下载这些更新后的文件。我的想法是:由最后修改时间判断该文件是不是最新的,然后下载更新的文件,这样可避免重复下载。各位有没有类似的程序例子?谢谢了!
3、关于文件路径的问题
有一种表示方法是“c:\\sim\\client”,还有一种“c:/sim/client”,这两个有什么区别么?

解决方案 »

  1.   

    这是我找的代码,可以实现,特此记录。
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */package sqlservertest.txt;/**
     *
     * @author Administrator
     */
    import java.sql.*;
    import java.io.*;
    import java.util.Calendar;
    public class DBTest {
       static String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
      static String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=gnss";
      static String user = "sa";
      static String passwd = "sa";
      static String filename ="D:\\001.ALM";
      public static void main(String[] args) throws Exception {
        Connection conn = null;
        try {
          Class.forName(driver);
          conn = DriverManager.getConnection(url,user,passwd);
      //    int op = 0;
          //插入
      //    if (op == 0){
          PreparedStatement ps = conn.prepareStatement("insert into FILE_ORIGENAL (create_date_time,content) values(?,?)");      ps.setString(1, "2010-11-3 08:41:10");
          InputStream in = new FileInputStream(filename);
          ps.setBinaryStream(2,in,in.available());
          ps.executeUpdate();
          ps.close();
          File f = new File(filename);
          System.out.println(f.getName());//文件名
          System.out.println(f.getPath());//路径名
          System.out.println();//创建时间      //获取文件最后修改的时间。Mon Apr 12 08:59:46 CST 2010
          Long time =f.lastModified();
          Calendar cd = Calendar.getInstance();
          cd.setTimeInMillis(time);
          System.out.println(cd.getTime());      //去掉后缀!
          //File file=new File("d:/test.txt");
          //System.out.println(f.getName().replaceAll("[.][^.]+$", ""));  //    }
          /*
          else {
          //取出
          PreparedStatement ps = conn.prepareStatement("select * from   tb_file where filename = ?");
          ps.setString(1, "aaa.exe");
          ResultSet rs = ps.executeQuery();
          rs.next();
          InputStream in = rs.getBinaryStream("filecontent");
          System.out.println(in.available());
          FileOutputStream out = new FileOutputStream("d:/bbb.exe");
          byte[] b = new byte[1024];
          int len = 0;
          while ( (len = in.read(b)) != -1) {
            out.write(b, 0, len);
            out.flush();
          }      out.close();
          in.close();
          rs.close();
          ps.close();
          }*/
        }
        catch (Exception ex) {
          ex.printStackTrace(System.out);
        }
        finally {
          try {conn.close();}
          catch (Exception ex) { }
        }
      }}
      

  2.   

    使用Quartz+Apache的FtpClient,很简单的。如果需要帮助请发站内信。
      

  3.   

    你可以,在本地做个文件记录的备份,与ftp上的文件进行比较,如果是最新的,就下载到本地更新.定时的可以在本地程序上,启一个线程,或Quartz也可以.
    package nc.ui.doc.doc_007;import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;import nc.itf.doc.DocDelegator;
    import nc.vo.doc.doc_007.DirVO;
    import nc.vo.pub.BusinessException;
    import nc.vo.pub.SuperVO;import org.apache.commons.net.ftp.FTPClient;
    import org.apache.commons.net.ftp.FTPFile;
    import org.apache.commons.net.ftp.FTPReply;
    import org.apache.commons.net.ftp.FTP;public class FtpTool {
    private FTPClient ftp; private String romateDir = ""; private String userName = ""; private String password = ""; private String host = ""; private String port = "21"; public FtpTool(String url) throws IOException {
    //String url="ftp://user:password@ip:port/ftptest/psd";
    int len = url.indexOf("//");
    String strTemp = url.substring(len + 2);
    len = strTemp.indexOf(":");
    userName = strTemp.substring(0, len);
    strTemp = strTemp.substring(len + 1); len = strTemp.indexOf("@");
    password = strTemp.substring(0, len);
    strTemp = strTemp.substring(len + 1);
    host = "";
    len = strTemp.indexOf(":");
    if (len < 0)//没有设置端口
    {
    port = "21";
    len = strTemp.indexOf("/");
    if (len > -1) {
    host = strTemp.substring(0, len);
    strTemp = strTemp.substring(len + 1);
    } else {
    strTemp = "";
    }
    } else {
    host = strTemp.substring(0, len);
    strTemp = strTemp.substring(len + 1);
    len = strTemp.indexOf("/");
    if (len > -1) {
    port = strTemp.substring(0, len);
    strTemp = strTemp.substring(len + 1);
    } else {
    port = "21";
    strTemp = "";
    }
    }
    romateDir = strTemp;
    ftp = new FTPClient();
    ftp.connect(host, FormatStringToInt(port)); } public FtpTool(String host, int port) throws IOException {
    ftp = new FTPClient();
    ftp.connect(host, port);
    } public String login(String username, String password) throws IOException {
    this.ftp.login(username, password);
    return this.ftp.getReplyString();
    } public String login() throws IOException {
    this.ftp.login(userName, password);
    System.out.println("ftp用户: " + userName);
    System.out.println("ftp密码: " + password);
    if (!romateDir.equals(""))
    System.out.println("cd " + romateDir);
    ftp.changeWorkingDirectory(romateDir);
    return this.ftp.getReplyString();
    } public  String upload(String pathname, String filename,String strPk_doc_dir) throws IOException, BusinessException { int reply;
    String m_sfilename = null;
    filename = CheckNullString(filename);
    if (filename.equals(""))
    return null;
    reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
    ftp.disconnect();
    System.out.println("FTP server refused connection.");
    System.exit(1);
    }
    FileInputStream is = null;
    try {
    File file_in;
    if (pathname.endsWith(File.separator)) {
    file_in = new File(pathname + filename);
    } else {
    file_in = new File(pathname + File.separator + filename);
    }
    if (file_in.length() == 0) {
    System.out.println("上传文件为空!");
    return null;
    }   
        //产生随机数最大到99   
    //     j = (int)(Math.random()*1000); 
        m_sfilename = strPk_doc_dir + ".pdf"; // 生成的文件名
    is = new FileInputStream(file_in);
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    ftp.storeFile(m_sfilename, is);
    ftp.logout();


    } finally {
    if (is != null) {
    is.close();
    }
    }
    System.out.println("上传文件成功!");
    System.out.println(m_sfilename);
    return m_sfilename;

    }


    public boolean delete(String filename) throws IOException {

    FileInputStream is = null;
    boolean retValue = false;
    try {
    retValue = ftp.deleteFile(filename);
    ftp.logout();
    } finally {
    if (is != null) {
    is.close();
    }
    }
    return retValue;

    } public void close() {
    if (ftp.isConnected()) {
    try {
    ftp.disconnect();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    } public static int FormatStringToInt(String p_String) {
    int intRe = 0;
    if (p_String != null) {
    if (!p_String.trim().equals("")) {
    try {
    intRe = Integer.parseInt(p_String);
    } catch (Exception ex) { }
    }
    }
    return intRe;
    } public static String CheckNullString(String p_String) {
    if (p_String == null)
    return "";
    else
    return p_String;
    }/* public boolean downfile(String pathname, String filename) {

    String outputFileName = null;
    boolean retValue = false;
    try {
    FTPFile files[] = ftp.listFiles();
    int reply = ftp.getReplyCode(); ////////////////////////////////////////////////
    if (!FTPReply.isPositiveCompletion(reply)) {
    try {
    throw new Exception("Unable to get list of files to dowload.");
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    /////////////////////////////////////////////////////
    if (files.length == 0) {
     System.out.println("No files are available for download.");
    }else {
     for (int i=0; i<files.length; i++) {
     System.out.println("Downloading file "+files[i].getName()+"Size:"+files[i].getSize());
     outputFileName = pathname + filename + ".pdf";
     //return outputFileName;
     File f = new File(outputFileName);
     
     //////////////////////////////////////////////////////
     retValue = ftp.retrieveFile(outputFileName, new FileOutputStream(f));
      if (!retValue) {
      try {
    throw new Exception ("Downloading of remote file"+files[i].getName()+" failed. ftp.retrieveFile() returned false.");
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
      }
     
    }
    }
     
    /////////////////////////////////////////////////////////////
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return retValue;
    }
    */
    public String downfile(String strLocalPath, String filename)throws IOException, BusinessException {
    String strAbsolutePath = null;
    FileOutputStream is = null;
    boolean success = false;
    try{
    File file_in;
    file_in = new File(strLocalPath + File.separator + filename);
    is = new FileOutputStream(file_in);
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    success=ftp.retrieveFile(filename, is);
    if(success == true){
    return strAbsolutePath = strLocalPath + File.separator + filename;
    }else{
    return strAbsolutePath;
    }

    }finally {
    if (is != null) {
    is.close();
    }
    }
    }
    public boolean CheckRemoteFileIsNotExist(String stRemotePath ,String filename){
    boolean NotExist=false;

    return NotExist;
    }
    }
      

  4.   

    你好,请问一下  ftpClient.getModificationTime(String pathname)抛出  Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 33 异常,是怎么解决的。我也遇到了这个问题,望指点,谢谢!
      

  5.   


    这是我当时的日报,是解决办法。希望对你有帮助。
    从ftp下载文件,自动获取最新文件成功。
       (long与String的转换,ini文件的读取,java建立数组的格式,FTPFile类的获取时间方式,long型数组比较大小)----------------------------------------------------------------
    思路:if(sourceFile.isFile()&&,日期 <=sourceFile.lastModified()   <=日期)   {下载   sourceFile 

    FTPFile[]   files   =   client.listFiles(); 
    for   (int   j   =   0;   j   <   files.length;   j++)   { 
    Date   fileDate   =   files[j].getTimestamp().getTime(); 
    //   todo... } 1、登录服务器后,获取服务器文件夹内所有文件的修改时间,求最大值,存在一个文件里,以备后用2、再次登陆后下载文件与这个时间对比,大于这个时间的才下载 long型数组比较大小