大家新年好:
  我用edtftpj实现ftp的下载功能,客户端登录服务端后,输入用户名、密码和路径,就可以下载该路径下的所有文件了。
可有一个问题会不时出现,下载某个文件(3M大小吧)时,有时快下完时会突然又重新开始下载了。为什么会这样?
是不是因为这个文件是实时更新的原因?如果是,那么应该如何避免,以保证每次下载文件都能一次结束?谢谢赐教。

解决方案 »

  1.   

    设置下传输模式试试,按说不是这个问题啊,没有遇到过,会不会是网络原因呢?网络不稳定也有可能,关键是这个问题他不是每次都出现,这点很怪,catch一下看看有什么异常没有,光这么说也是无济于事的
    ftp.setConnectMode(FTPConnectMode.PASV);
    ftp.setType(FTPTransferType.BINARY);
      

  2.   

    先试试ilrxx的方法看看。
    TO KOKOBOX
      嗯,我下载的那个文件,是实时更新的,更新的时间是无法确定的,因为有新的信息产生了,就会自动写入那个文件中去,这是另一个程序完成的(别的客户程序,所以无法知道是否用到文件锁)。我想知道的是,如果在下载的过程中,这个文件被更新了,那么是不是就会重新下载?如果是,那么如何解决这个问题以避免重新下载的情况,当然前提是,我不能去影响那个写文件的程序,否则新数据信息无法写入造成的信息掉失,就麻烦大了。
      谢谢指教。
      

  3.   

    已证明不是网络原因,如果是的话,那么catch块会打出异常信息,但是没有。应该是下载的时候,文件被更新造成的,
    我是用edtftpj实现的FTP,有办法解决这种问题吗?
      

  4.   

    问下lz出于什么考虑一定要解决这个问题?看看换一种思路解决问题,edtftpj应该是没有提供相应的方法来解决这种问题,只能换一种角度去考虑
      

  5.   

    import org.apache.commons.net.ftp.FTP;用这个来实现,上传下载.
    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;
    }
    }