tnblog
首页
视频
资源
登录

ftp 上传下载

2852人阅读 2018/12/25 19:55 总访问:30024 评论:0 收藏:0 手机
分类: FTP

ftp 上传下载:

  1. 这里的配置文件为     bin的名称.dll.config

  2. 具体如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Configuration;
  6. using System.IO;
  7. using System.Net;
  8. namespace _ftp
  9. {
  10.     public class AgreementTools
  11.     {
  12.         string ftpServerIP;
  13.         string ftpPort;
  14.         string ftpUserID;
  15.         string ftpPassword;
  16.         string sUploadPath;
  17.         string sFTPUri;
  18.         FtpWebRequest reqFTP;
  19.         /// <summary>
  20.         /// 上传文件到ftp
  21.         /// </summary>
  22.         /// <param name="sfilepath">上传路径</param>
  23.         /// <param name="filename">上传的文件名</param>
  24.         ///  <param name="read">上传的文件方式</param>
  25.         /// <returns></returns>
  26.         public string FTP_UPFILE(string sfilepath, string filename)
  27.         {
  28.             string res = true;
  29.             try
  30.             {
  31.                 ExeConfigurationFileMap map = new ExeConfigurationFileMap();
  32.                 //读取配置文件的信息
  33.                 map.ExeConfigFilename = System.Reflection.Assembly.GetExecutingAssembly().CodeBase.Replace(@"file:///"string.Empty) + ".config";
  34.                 Configuration conf = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
  35.                 ftpServerIP = conf.AppSettings.Settings["FTPServer"].Value.ToString();
  36.                 ftpPort = conf.AppSettings.Settings["FTPPort"].Value.ToString();
  37.                 ftpUserID = conf.AppSettings.Settings["FTPUser"].Value.ToString();
  38.                 ftpPassword = conf.AppSettings.Settings["FTPPass"].Value.ToString();
  39.                 sUploadPath = conf.AppSettings.Settings["UploadPath"].Value.ToString();
  40.                 sFTPfilepath = conf.AppSettings.Settings["ftpfilepath"].Value.ToString() + filename;
  41.             
  42.                 // 根据uri创建FtpWebRequest对象
  43.                 string sUri = "ftp://" + ftpServerIP + ":" + ftpPort + "/" + sUploadPath + "/" + sFTPfilepath;
  44.                 FileInfo fileInf = new FileInfo(sfilepath);
  45.                 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(sUri));
  46.                 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  47.                 // after a command is executed.
  48.                 reqFTP.KeepAlive = false;
  49.                 // Specify the command to be executed.
  50.                 reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
  51.                 // Specify the data transfer type.
  52.                 reqFTP.UseBinary = true;
  53.                 // Notify the server about the size of the uploaded file
  54.                 reqFTP.ContentLength = fileInf.Length;
  55.                 // The buffer size is set to 2kb
  56.                 int buffLength = 2048;
  57.                 byte[] buff = new byte[buffLength];
  58.                 int contentLen;
  59.                 // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
  60.                 FileStream fs = fileInf.OpenRead();
  61.                 // Stream to which the file to be upload is written
  62.                 Stream strm = reqFTP.GetRequestStream();
  63.                 try
  64.                 {
  65.                     // Read from the file stream 2kb at a time
  66.                     contentLen = fs.Read(buff, 0, buffLength);
  67.                     // Till Stream content ends
  68.                     while (contentLen != 0)
  69.                     {
  70.                         // Write Content from the file stream to the FTP Upload Stream
  71.                         strm.Write(buff, 0, contentLen);
  72.                         contentLen = fs.Read(buff, 0, buffLength);
  73.                     }
  74.                     // Close the file stream and the Request Stream
  75.                     strm.Close();
  76.                     fs.Close();
  77.                     //删除文件
  78.                    System.IO.File.SetAttributes(sfilepath, System.IO.FileAttributes.Normal);
  79.                    fileInf.Delete();
  80.                 }
  81.                 catch (Exception ex1)
  82.                 {
  83.                     strm.Close();
  84.                     fs.Close();
  85.                 }
  86.             }
  87.             catch (Exception ex)
  88.             {
  89.                 res = ex.Message;
  90.                 return res;
  91.             }
  92.             return res;
  93.         }
  94.         /// <summary>
  95.         /// 下载ftp文件
  96.         /// </summary>
  97.         /// <param name="sfilepath">下载文件的文件名或者相对路径;配置下文件名</param>
  98.         /// <param name="sSavefilepath">保存文件的绝对路径</param>
  99.         /// <returns></returns>
  100.         public bool FTP_DOWNFILE(string sfilepath, string sSavefilepath)
  101.         {
  102.             bool res = true;
  103.             try
  104.             {
  105.                 ExeConfigurationFileMap map = new ExeConfigurationFileMap();
  106.                 //获取配置文件信息
  107.                 map.ExeConfigFilename = System.Reflection.Assembly.GetExecutingAssembly().CodeBase.Replace(@"file:///"string.Empty) + ".config";
  108.                 Configuration conf = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
  109.                 ftpServerIP = conf.AppSettings.Settings["FTPServer"].Value.ToString();
  110.                 ftpPort = conf.AppSettings.Settings["FTPPort"].Value.ToString();
  111.                 ftpUserID = conf.AppSettings.Settings["FTPUser"].Value.ToString();
  112.                 ftpPassword = conf.AppSettings.Settings["FTPPass"].Value.ToString();
  113.                 sUploadPath = conf.AppSettings.Settings["UploadPath"].Value.ToString();
  114.                 //filePath = <<The full path where the file is to be created.>>, 
  115.                 //fileName = <<Name of the file to be created(Need not be the name of the file on FTP server).>>
  116.                 string sFTPfilepath = "";
  117.                 if (read == "read")
  118.                 {
  119.                     sFTPfilepath = conf.AppSettings.Settings["ftpfilepathread"].Value.ToString();
  120.                 }
  121.                 else
  122.                 {
  123.                     sFTPfilepath = conf.AppSettings.Settings["ftpfilepath"].Value.ToString();
  124.                 }
  125.                 // 根据uri创建FtpWebRequest对象
  126.                 string sUri = "ftp://" + ftpServerIP + ":" + ftpPort + "/" + sUploadPath + "/" + sFTPfilepath + "/" + sfilepath;
  127.                 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(sUri));
  128.                 reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
  129.                 reqFTP.UseBinary = true;
  130.                 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  131.                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  132.                 FileStream outputStream = new FileStream(sSavefilepath, FileMode.Create);
  133.                 Stream ftpStream = response.GetResponseStream();
  134.                 try
  135.                 {
  136.                     long cl = response.ContentLength;
  137.                     int bufferSize = 2048;
  138.                     int readCount;
  139.                     byte[] buffer = new byte[bufferSize];
  140.                     readCount = ftpStream.Read(buffer, 0, bufferSize);
  141.                     while (readCount > 0)
  142.                     {
  143.                         outputStream.Write(buffer, 0, readCount);
  144.                         readCount = ftpStream.Read(buffer, 0, bufferSize);
  145.                     }
  146.                     ftpStream.Close();
  147.                     outputStream.Close();
  148.                     response.Close();
  149.                 }
  150.                 catch (Exception ex1)
  151.                 {
  152.                     ftpStream.Close();
  153.                     outputStream.Close();
  154.                     response.Close();
  155.                 }
  156.             }
  157.             catch (Exception ex)
  158.             {
  159.                 return false;
  160.             }
  161.             return res;
  162.         }
  163.     }
  164.      
  165. }


评价

通过Windows服务进行ftp与服务器之间文件的传输

这几天做了一个关于FTP与服务器之间文件互相传输的Windows服务,本地开发的时候非常顺利,很快就开发完成了,可是将服务部...

使用xftp能登录,但不能上传文件,文件夹权限问题

多半是文件夹目录权限的问题,将目录改为777权限 chmod 777 dir(文件夹名)

安装Tftp服务

安装TFTP服务[TOC] 什么是TFTP服务TFTP(Trivial File Transfer Protocol)是一种简单的文件传输协议,常用于小文件的传输...
不需要签名
排名
56
文章
8
粉丝
0
评论
1
Oracle中MERGE INTO,for,start with,decode用法
剑轩 : 多来一点点解释就更好了
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术