当前位置 博文首页 > scftp下载,Http和FTP下载文件

    scftp下载,Http和FTP下载文件

    作者:xiaoyingying 时间:2021-08-14 18:40

        IIS7批量FTP管理功能说明:
        1、可批量导入,导出FTP信息
        2、其他ftp工具有的功能,我们也有
        3、特色功能:可以定时上传下载
        4、数据信息列表化、一眼就能知道那个是那个
        5、批量连接标签页式切换方便快捷
        6、7大连接模式更多好的兼容
        7、内嵌编辑器有效解决普通txt记事本乱码
        8、锁屏功能当程序有规定时间内没人操作,则自动锁程序。输入密码才可以正常操作
        本产品适用于:懒得记录FTP信息和有批量定时备份,上传下载的运维或站长。
        下载地址:http://ftp.iis7.com/
        图片:
       
        说起下载文件,大家都会想起http和FTP下载。http和ftp,其实底层都是基于socket通信,只不过http和ftp协议格式定义不一样而已。
        下载文件,要看服务器支持什么类型的协议,如果只支持http,那你就用httpwebrequest类好了,如果支持FTP,那你就用FTP的类来下载文件。
        下面是我对两者的总结使用。
        http:
     
        ///<summary>下载安装包</summary>
        publicvoiddownloadPack(stringmacCode,stringappType,stringaddress)
        {
        StringBuilderdata=newStringBuilder();
        data.Append("{\"cpu\":\""+macCode+"\"");
        data.Append(",\"appType\":\""+appType+"\"}");
        //data.Append(",\"softwareVersion\":\""+iniFile.IniReadValue("USER","versionNo")+"\"");
        ServicePointManager.ServerCertificateValidationCallback=ValidateServerCertificate;
        HttpWebRequestrequest=(HttpWebRequest)WebRequest.Create(address);
        request.Headers.Add("X-Auth-Token",HttpUtility.UrlEncode("openstack"));
        request.Method="POST";
        request.ContentType="application/json";
        request.Accept="application/json";
        byte[]byteData=Encoding.UTF8.GetBytes(data.ToString());
        //把要下载的http的文件的相关信息告诉后台
        using(StreampostStream=request.GetRequestStream())
        {
        postStream.Write(byteData,0,byteData.Length);
        }
        Log.AddLog("Download","2");
        sc.start();
        process=0;
        using(HttpWebResponseresponse=(HttpWebResponse)request.GetResponse())
        {
        Log.AddLog("Download","3");
        intall_count=0;
        using(Streamreader=response.GetResponseStream())
        {
        Log.AddLog("Download",filePath);
        using(FileStreamfw=newFileStream(filePath,FileMode.Create,FileAccess.Write))
        {
        Log.AddLog("Download","5");
        byte[]buffer=newbyte[1024*10];
        while(true)
        {
        Log.AddLog("Download",all_count.ToString());
        intcount=reader.Read(buffer,0,buffer.Length);
        all_count+=count;
        fw.Write(buffer,0,count);
        sc.setSize(all_count);
        writeCount+=(ulong)count;
        process=(float)writeCount/(float)fileSize;
        if(count<=0)
        {
        break;
        }
        }
        isFinish=true;
        sc.stop();
        }
        }
        }
        }
      
        FTP:
     
        staticstringdownloadFile(stringfileName)
        {
        stringerror="";
        intsumSize=0;
        process=0;
        //下载后的文件存放路径
        stringdownloadUrl=getBasePath()+fileName;
        //需要现在的文件在ftp上的完整路径
        stringfileUploadPath=ftpServer+fileName;
        Uriuri=newUri(fileUploadPath);
        //创建文件流
        FileStreamfs=null;
        StreamresponseStream=null;
        try
        {
        //创建一个与FTP服务器联系的FtpWebRequest对象
        FtpWebRequestrequest=(FtpWebRequest)WebRequest.Create(uri);
        //设置请求的方法是FTP文件下载
        request.Method=WebRequestMethods.Ftp.DownloadFile;
        //连接登录FTP服务器
        request.Credentials=newNetworkCredential(ftpUserName,ftpUserPwd);
        //获取一个请求响应对象
        FtpWebResponseresponse=(FtpWebResponse)request.GetResponse();
        //获取请求的响应流
        responseStream=response.GetResponseStream();
        //判断本地文件是否存在,如果存在,则打开和重写本地文件
        if(File.Exists(downloadUrl))
        {
        fs=File.Open(downloadUrl,FileMode.Open,FileAccess.ReadWrite);
        }
        else
        {
        fs=File.Create(downloadUrl);
        }
        if(fs!=null)
        {
        intbuffer_count=65536;
        byte[]buffer=newbyte[buffer_count];
        intsize=0;
        while((size=responseStream.Read(buffer,0,buffer_count))>0)
        {
        fs.Write(buffer,0,size);
        sumSize+=size;
        process=sumSize*1.0/allSize;
        }
        fs.Flush();
        fs.Close();
        responseStream.Close();
        }
        }
        catch(Exceptione)
        {
        error="自动更新服务异常:"+e.ToString();
        }
        finally
        {
        if(fs!=null)
        fs.Close();
        if(responseStream!=null)
        responseStream.Close();
        }
        returnnull;
        }
        原文:https://www.cnblogs.com/NangFah/p/14813863.html