axacropdf 服务器pdf_ftp获取远程Pdf文件

usingSystem;usingSystem.Collections.Generic;usingSystem.Globalization;usingSystem.IO;usingSystem.Linq;usingSystem.Net;usingSystem.Text;usingSystem.Text.RegularExpressions;namespacemyPDFDmeo

{enumFileListStyle

{

UnixStyle,

WindowsStyle,

Unknown

}///

///文件信息///

public classFileStruct

{public string Flags { get; set; }public bool IsDirectory { get; set; }public string Owner { get; set; }public string Group { get; set; }public string Size { get; set; }public DateTime CreateTime { get; set; }public string Name { get; set; }

}///

///Ftp访问文件目录类///

classFtpUtility

{//ftp 用户名

private stringusername;//ftp密码

private stringpassword;//ftp文件访问地址

private stringuri;privateFileInfo fileInfo;///

///初始化Ftp///

///

///

///

public void FtpExplorer(string username, string password, stringuri)

{this.username =username;this.password =password;this.uri =uri;

}///

///获取本地目录文件名///

///

///

public List ProcessDirectory(stringlocalFile)

{

List fileNames = new List();string[] fileEntries =Directory.GetFiles(localFile);for (int i = 0; i < fileEntries.Length; i++)

{

fileInfo= newFileInfo(fileEntries[i].ToString());

fileNames.Add(fileInfo.Name);

}returnfileNames;

}#region ftp下载浏览文件夹信息

///

///得到当前目录下的所有目录和文件///

/// 浏览的目录

///

public ListGetFileList( )

{

List list = new List();

FtpWebRequest reqFtp;

WebResponse response= null;string ftpuri = string.Format("ftp://{0}/", uri);try{

reqFtp=(FtpWebRequest)FtpWebRequest.Create(ftpuri);

reqFtp.UseBinary= true;

reqFtp.Credentials= newNetworkCredential(username, password);

reqFtp.Method=WebRequestMethods.Ftp.ListDirectoryDetails;

response=reqFtp.GetResponse();

list=ListFilesAndDirectories((FtpWebResponse)response).ToList();

response.Close();

}catch{if (response != null)

{

response.Close();

}

}returnlist;

}#endregion

#region 列出目录文件信息

///

///列出FTP服务器上面当前目录的所有文件和目录///

publicFileStruct[] ListFilesAndDirectories(FtpWebResponse Response)

{

StreamReader stream= newStreamReader(Response.GetResponseStream(), Encoding.Default);string Datastring =stream.ReadToEnd();

FileStruct[] list=GetList(Datastring);returnlist;

}///

///获得文件和目录列表///

/// FTP返回的列表字符信息

private FileStruct[] GetList(stringdatastring)

{

List myListArray = new List();string[] dataRecords = datastring.Split('\n');

FileListStyle _directoryListStyle=GuessFileListStyle(dataRecords);foreach (string s indataRecords)

{if (_directoryListStyle != FileListStyle.Unknown && s != "")

{

FileStruct f= newFileStruct();

f.Name= "..";switch(_directoryListStyle)

{caseFileListStyle.UnixStyle:

f=ParseFileStructFromUnixStyleRecord(s);break;caseFileListStyle.WindowsStyle:

f=ParseFileStructFromWindowsStyleRecord(s);break;

}if (!(f.Name == "." || f.Name == ".."))

{

myListArray.Add(f);

}

}

}returnmyListArray.ToArray();

}///

///从Windows格式中返回文件信息///

/// 文件信息

private FileStruct ParseFileStructFromWindowsStyleRecord(stringRecord)

{

FileStruct f= newFileStruct();string processstr =Record.Trim();string dateStr = processstr.Substring(0, 8);

processstr= (processstr.Substring(8, processstr.Length - 8)).Trim();string timeStr = processstr.Substring(0, 7);

processstr= (processstr.Substring(7, processstr.Length - 7)).Trim();

DateTimeFormatInfo myDTFI= new CultureInfo("en-US", false).DateTimeFormat;

myDTFI.ShortTimePattern= "t";

f.CreateTime= DateTime.Parse(dateStr + " " +timeStr, myDTFI);if (processstr.Substring(0, 5) == "

{

f.IsDirectory= true;

processstr= (processstr.Substring(5, processstr.Length - 5)).Trim();

}else{string[] strs = processstr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); //true);

processstr = strs[1];

f.IsDirectory= false;

}

f.Name=processstr;returnf;

}///

///判断文件列表的方式Window方式还是Unix方式///

/// 文件信息列表

private FileListStyle GuessFileListStyle(string[] recordList)

{foreach (string s inrecordList)

{if (s.Length > 10

&& Regex.IsMatch(s.Substring(0, 10), "(-|d)(-|r)(-|w)(-|x)(-|r)(-|w)(-|x)(-|r)(-|w)(-|x)"))

{returnFileListStyle.UnixStyle;

}else if (s.Length > 8

&& Regex.IsMatch(s.Substring(0, 8), "[0-9][0-9]-[0-9][0-9]-[0-9][0-9]"))

{returnFileListStyle.WindowsStyle;

}

}returnFileListStyle.Unknown;

}///

///从Unix格式中返回文件信息///

/// 文件信息

private FileStruct ParseFileStructFromUnixStyleRecord(stringRecord)

{

FileStruct f= newFileStruct();string processstr =Record.Trim();

f.Flags= processstr.Substring(0, 10);

f.IsDirectory= (f.Flags[0] == 'd');

processstr= (processstr.Substring(11)).Trim();

_cutSubstringFromStringWithTrim(ref processstr, ' ', 0); //跳过一部分

f.Owner = _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);

f.Group= _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);

f.Size= _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);string yearOrTime = processstr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[2];int m_index =processstr.IndexOf(yearOrTime);if (yearOrTime.IndexOf(":") >= 0) //time

{

processstr=processstr.Replace(yearOrTime, DateTime.Now.Year.ToString());

}

f.CreateTime= DateTime.Parse(_cutSubstringFromStringWithTrim(ref processstr, ' ', 8) + " " +yearOrTime);

f.Name= processstr; //最后就是名称

returnf;

}///

///按照一定的规则进行字符串截取///

/// 截取的字符串

/// 查找的字符

/// 查找的位置

private string _cutSubstringFromStringWithTrim(ref string s, char c, intstartIndex)

{int pos1 =s.IndexOf(c, startIndex);string retString = s.Substring(0, pos1);

s=(s.Substring(pos1)).Trim();returnretString;

}#endregion

///

///Ftp下载文档///

public int DownloadFtp(string filePath, string fileName, string ftpServerIP, string ftpUserID, stringftpPassword)

{

FtpWebRequest reqFTP;try{

FileStream outputStream= new FileStream(filePath + "\\" +fileName, FileMode.Create);

reqFTP= (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" +fileName));

reqFTP.Method=WebRequestMethods.Ftp.DownloadFile;

reqFTP.UseBinary= true;

reqFTP.KeepAlive= false;

reqFTP.Credentials= newNetworkCredential(ftpUserID, ftpPassword);

FtpWebResponse response=(FtpWebResponse)reqFTP.GetResponse();

Stream ftpStream=response.GetResponseStream();long cl =response.ContentLength;int bufferSize = 2048;intreadCount;byte[] buffer = new byte[bufferSize];

readCount= ftpStream.Read(buffer, 0, bufferSize);while (readCount > 0)

{

outputStream.Write(buffer,0, readCount);

readCount= ftpStream.Read(buffer, 0, bufferSize);

}

ftpStream.Close();

outputStream.Close();

response.Close();return 0;

}catch(Exception ex)

{return -2;

}

}public stringUsername

{get { returnusername; }set { username =value; }

}public stringPassword

{get { returnpassword; }set { password =value; }

}public stringUri

{get { returnuri; }set { uri =value; }

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值