dlmh_system/DataService/Utility/FTPHelper.cs

76 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Net;
using System.Reflection;
public class FTPHelper
{
private static readonly string ftpIp = ConfigurationManager.AppSettings["FtpIp"];
private static readonly string port = ConfigurationManager.AppSettings["Port"];
private static readonly string user = ConfigurationManager.AppSettings["User"];
private static readonly string passWord = ConfigurationManager.AppSettings["PassWord"];
///// <summary>
///// FTP的服务器地址格式为ftp://192.168.1.234:8021/。
///// </summary>
//private string FTPCONSTR = ConfigurationManager.AppSettings["Test"];
///// <summary>
///// //FTP服务器的用户名
///// </summary>
//private string FTPUSERNAME { get; set; }
///// <summary>
///// //FTP服务器的密码
///// </summary>
//private string FTPPASSWORD { get; set; }
//public FTPHelper(string ip, string username, string password, string port = "21")
//{
// FTPCONSTR = string.Format("{0}://{1}:{2}/", "ftp", ip, port);
// FTPUSERNAME = username;
// FTPPASSWORD = password;
//}
#region FTP服务器
/// <summary>
/// 上传文件到远程ftp
/// </summary>
/// <param name="path">本地的文件目录</param>
/// <param name="name">文件名称</param>
/// <returns></returns>
public bool UploadFile(byte[] data,string fileName)
{
//FileInfo f = new FileInfo(path);
string path = string.Format("{0}://{1}:{2}/", "ftp", ftpIp, port) + fileName;//这个路径是我要传到ftp目录下的这个目录下
FtpWebRequest reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
reqFtp.Method = WebRequestMethods.Ftp.UploadFile;
reqFtp.UsePassive = false;//只需要添加这一句话
reqFtp.UseBinary = true;
reqFtp.Credentials = new NetworkCredential(user, passWord);
reqFtp.KeepAlive = false;
reqFtp.Method = WebRequestMethods.Ftp.UploadFile;
reqFtp.ContentLength = data.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
//FileStream fs = f.OpenRead();
try
{
Stream strm = reqFtp.GetRequestStream();
while (data.Length != 0)
{
strm.Write(data, 0, data.Length);
}
strm.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
#endregion
}