javaftp断点(java ftp 断点续传)

华为云服务器特价优惠火热进行中!

2核2G2兆仅需 38 元;4核4G3兆仅需 79 元。购买时间越长越优惠!更多配置及优惠价格请咨询客服。

合作流程:
1、点击链接注册/关联华为云账号:点击跳转
2、添加客服微信号:cloud7591,确定产品方案、价格方案、服务支持方案等;
3、客服协助购买,并拉微信技术服务群,享受一对一免费技术支持服务;
技术专家在金蝶、华为、腾讯原厂有多年工作经验,并已从事云计算服务8年,可对域名、备案、网站搭建、系统部署、AI人工智能、云资源规划等上云常见问题提供更专业靠谱的服务,对相应产品提供更优惠的报价和方案,欢迎咨询。

本篇文章给大家谈谈javaftp断点,以及java ftp 断点续传对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

微信号:cloud7591
如需了解更多,欢迎添加客服微信咨询。
复制微信号

本文目录一览:

java ftp怎么实现java ftp方式的断点续传

运用类的办法,编程人员能够长途登录到FTP服务器,罗列该服务器上的目录,设置传输协议,以及传送文件。FtpClient类涵 盖了简直一切FTP的功用,FtpClient的实例变量保留了有关树立"署理"的各种信息。下面给出了这些实例变量:

public static boolean useFtpProxy

这个变量用于标明FTP传输过程中是不是运用了一个署理,因此,它实际上是一个符号,此符号若为TRUE,标明运用了一个署理主机。

public static String ftpProxyHost

此变量只要在变量useFtpProxy为TRUE时才有用,用于保留署理主机名。

public static int ftpProxyPort

此变量只要在变量useFtpProxy为TRUE时才有用,用于保留署启孙旁理主机的端口地址。

FtpClient有三种不同方式的结构函数,如下所示:

1、public FtpClient(String hostname,int port)

此结构函数运用给出的主机名和端口号树立一条FTP衔接。

2、public FtpClient(String hostname)

此结构函数运用给出的主机名树立一条FTP衔接,运用默许端口号。

3、FtpClient()

此结构函数将创立一FtpClient类,但不树立FTP衔接。这时,FTP衔接能够用openServer办法树立。

一旦树立了类FtpClient,就能够用这个类的办法来翻开与FTP服务器的衔接。类ftpClient供给了如下两个可用于翻开与FTP服务器之间的衔接的办法。

public void openServer(String hostname)

这悄橡个办法用于凯激树立一条与指定主机上的FTP服务器的衔接,运用默许端口号。

什么是FTP断点续传?

FTP客户端软件断点续传指的是在下载或上传时,将下载或上传任务(一个文件或一个压缩包)人为的划分为几个部分,每一个部分采用一个线程进行上传或下载,如果碰到网络故障,可以从已经上传或下载的部分开始继续上传下载以后未上传下载的部分,而没有必要重头开始上绝磨如传下载。用途可以节省时间,提高速度。

有时用户上传下载文件需要历时数小时,万一线路中断,不具备断点续传的FTP服务器或下载软件就只能从头重传;比较好的FTP服务器或下载软件具有FTP断点续传能力,允许用户从上传下载断线的地方继续传动,这样大大减少了用户的烦恼。

常游芹见的支持断点续传的上传、下载软件并启:超级旋风、迅雷、web迅雷、影音传送带、快车、BitComet、电驴eMule、哇嘎Vagaa等。

请问用java写FTP,如果想设置当连接失败后隔一段时间重连,该如何写?

你好,根据你的描述,我觉得可以使用Timer定时器实现。

代码可以这么写吧,具体要写什么内容,就者碧看你首罩举的要求了。

if(连接失败闷桐)

{

Timer timer = new Timer(); timer.schedule(new DoFTPConnect(), 0, 30 * 60*1000);

}

希望对你有帮助!

java上传文件到ftp服务器时出错,求高手回答

你先输出一下橡咐毁System.out.println(ftpClient.isConnected());看看是否连简拍接啊,是梁备否进入if语句了么!最简单找原因的办法就是debug断点调试了

java如何测试连接ftp是否通

java测试连接ftp是否连通可以使用判断是否有异常来决定,实例如下:

/** 

     * connectServer 

     * 连接ftp服务器 

     * @throws java.io.IOException 

     * @param path 文件夹,空代表根目录 

     * @param password 密码 

     * @param user    登陆用户 

 启困    * @param server 服务器地址 

     */ 

  public void connectServer(String server, String user, String password,  String path) 

  throws IOException 

   { 

     // server:FTP服务器的IP地址;user:登录FTP服务器的用户名 

     // password:登录FTP服务器的用户名的口令;path:FTP服务器上的路径 

      ftpClient = new FtpClient(); 

      ftpClient.openServer(server); 

      ftpClient.login(user, password); 

     //path是ftp服务下主目录的子目录 

     if (path.length() != 0)   ftpClient.cd(path); 

     //用2进制上传、下载 

      ftpClient.binary();      

/** 

     * upload 

     * 上传文件 

     * @throws java.lang.Exception 

     * @return -1 文件不存在 

     *           -2 文件内容为空 

     *           0 成功上传,返回文件的大小 

     * @param newname 上传后的新文件名 

     * @param filename 上传的文件 

     */ 

public long upload(String filename,String newname) throws Exception 

     long result = 0; 

      TelnetOutputStream os = null; 

      FileInputStream is = null; 

     try {          

          java.io.File file_in = new java.io.File(filename); 

         if (!file_in.exists()) return -1; 

         if (file_in.length()==0) return -2; 

          os = ftpClient.put(newname); 

          result = file_in.length(); 

          is = new FileInputStream(file_in); 

         byte[] bytes = new byte[1024]; 

         int c; 

         while ((c = is.read(bytes)) != -1) { 

               os.write(bytes, 0, c); 

          } 

      } finally { 

         if (is != null) { 

              is.close(); 

          } 

         if (os != null) { 

             os.close(); 

          } 

      } 

    return result; 

/** 

     * upload 

  局基   * @throws java.lang.Exception 

     * @return 

     * @param filename 

     */ 

public long upload(String filename) 

throws Exception 

    String newname = ""; 

    if (filename.indexOf("/")-1) 

     { 

        newname = filename.substring(filename.lastIndexOf("/")+1); 

     }else 

     { 

        newname = filename; 

     } 

    return upload(filename,newname); 

/** 

     *   download 

     *   从ftp下载文件到本地 

     * @throws java.lang.Exception 

     * @return 

     * @param newfilename 本地生成的文件名 

     * @param filename 服务器上的文件名 

     */ 

public long download(String filename,String newfilename) 

throws Exception 

{   

    long result = 0; 

     TelnetInputStream is = null; 

     FileOutputStream os = null; 

    try 

     { 

        is = ftpClient.get(filename);        

        java.io.File outfile = new java.io.File(newfilename); 

        os = new FileOutputStream(outfile); 

       byte[] bytes = new byte[1024]; 

       int c; 

       while ((c = is.read(bytes)) != 桐旁谨-1) { 

            os.write(bytes, 0, c); 

            result = result + c; 

        } 

     } catch (IOException e) 

     { 

        e.printStackTrace(); 

     } 

    finally { 

         if (is != null) { 

              is.close(); 

          } 

         if (os != null) { 

             os.close(); 

          } 

      } 

     return result; 

/** 

   * 取得某个目录下的所有文件列表 

   * 

   */ 

public List getFileList(String path) 

     List list = new ArrayList(); 

    try 

     { 

        DataInputStream dis = new   DataInputStream(ftpClient.nameList(path)); 

       String filename = ""; 

       while((filename=dis.readLine())!=null)   

        {   

          list.add(filename);         

        }   

    

     } catch (Exception e) 

     { 

        e.printStackTrace(); 

     } 

    return list; 

/** 

     * closeServer 

     * 断开与ftp服务器的链接 

     * @throws java.io.IOException 

     */ 

public void closeServer() 

throws IOException 

{    

   try 

    { 

      if (ftpClient != null) 

       { 

         ftpClient.closeServer();      

       } 

    } catch (IOException e) { 

       e.printStackTrace(); 

    } 

   

  public static void main(String [] args) throws Exception 

   { 

     FtpUtil ftp = new FtpUtil(); 

    try { 

         //连接ftp服务器 

          ("10.163.7.15", "cxl", "1", "info2"); 

         /**   上传文件到 info2 文件夹下 */ 

          System.out.println("filesize:"+("f:/download/Install.exe")+"字节"); 

         /** 取得info2文件夹下的所有文件列表,并下载到 E盘下 */ 

          List list = ("."); 

         for (int i=0;ilist.size();i++) 

          { 

            String filename = (String)list.get(i); 

             System.out.println(filename); 

             (filename,"E:/"+filename); 

          } 

     } catch (Exception e) { 

       /// 

     }finally 

     { 

        ; 

     } 

   }   

}

java ftp上传时断网,文件损坏

以二进制流上传,然后实现断点续传。

/**

* 上传文件到FTP服务器,支持断点续传

* @param local 本地文件名称,绝对路径

* @param remote 远程文件路径,使用/home/directory1/subdirectory/file.ext 按照Linux上的路径指定方式,支持多级目录嵌套,支持递归创建不存在的目录结构

* @return 上传结果

* @throws IOException

*/

public UploadStatus upload(String local,String remote) throws IOException{

FTPClient ftpClient = new FTPClient();

//设置PassiveMode传输

ftpClient.enterLocalPassiveMode();

//设置以二进制流的方式传输

ftpClient.setFileType();

UploadStatus result;

//对远程目录的处理

String remoteFileName = remote;

if(remote.contains("/")){

remoteFileName = remote.substring(remote.lastIndexOf("/")+1);

String directory = remote.substring(0,remote.lastIndexOf("/")+1);

if(!directory.equalsIgnoreCase("/")!ftpClient.changeWorkingDirectory(directory)){

//如果远程目录不存在,则递归山历创建远程逗念搜服务器目录

int start=0;

int end = 0;

if(directory.startsWith("/")){

start = 1;

}else{

start = 0;

}

end = directory.indexOf("/",start);

while(true){

String subDirectory = remote.substring(start,end);

if(!ftpClient.changeWorkingDirectory(subDirectory)){

if(ftpClient.makeDirectory(subDirectory)){

ftpClient.changeWorkingDirectory(subDirectory);

}else {

System.out.println("创建目录高野失败");

return UploadStatus.Create_Directory_Fail;

}

}

start = end + 1;

end = directory.indexOf("/",start);

//检查所有目录是否创建完毕

if(end = start){

break;

}

}

}

}

//检查远程是否存在文件

FTPFile[] files = ftpClient.listFiles(remoteFileName);

if(files.length == 1){

long remoteSize = files[0].getSize();

File f = new File(local);

long localSize = f.length();

if(remoteSize==localSize){

return UploadStatus.File_Exits;

}else if(remoteSize localSize){

return UploadStatus.Remote_Bigger_Local;

}

//尝试移动文件内读取指针,实现断点续传

InputStream is = new FileInputStream(f);

if(is.skip(remoteSize)==remoteSize){

ftpClient.setRestartOffset(remoteSize);

if(ftpClient.storeFile(remote, is)){

return UploadStatus.Upload_From_Break_Success;

}

}

//如果断点续传没有成功,则删除服务器上文件,重新上传

if(!ftpClient.deleteFile(remoteFileName)){

return UploadStatus.Delete_Remote_Faild;

}

is = new FileInputStream(f);

if(ftpClient.storeFile(remote, is)){

result = UploadStatus.Upload_New_File_Success;

}else{

result = UploadStatus.Upload_New_File_Failed;

}

is.close();

}else {

InputStream is = new FileInputStream(local);

if(ftpClient.storeFile(remoteFileName, is)){

result = UploadStatus.Upload_New_File_Success;

}else{

result = UploadStatus.Upload_New_File_Failed;

}

is.close();

}

return result;

}

javaftp断点的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java ftp 断点续传、javaftp断点的信息别忘了在本站进行查找喔。

发布于 2023-04-14 16:04:41
收藏
分享
海报
41
目录

    忘记密码?

    图形验证码

    复制成功
    微信号: cloud7591
    如需了解更多,欢迎添加客服微信咨询。
    我知道了