1 FTP连接辅助类
public class TFTPOperationUnit { private string FFTPUrl; string FFTPUserName; string FFTPPwd; private NetworkCredential fNetworkCredential; public NetworkCredential FNetworkCredential{ get{ if ( fNetworkCredential == null ) { fNetworkCredential = new NetworkCredential ( FFTPUserName, FFTPPwd) ; } return fNetworkCredential; } } public TFTPOperationUnit ( string aUrl = "" , string aUserName = "" , string aPwd = "" ) { FFTPUrl = aUrl; FFTPUserName = aUserName; FFTPPwd = aPwd; } public string[ ] GetFolderPathFiles ( string aFolderName ) { string bFTPFolderPath = FFTPUrl + aFolderName; string[ ] bFolderPathFiles = new string [ ] { } ; StringBuilder bReadStr = new StringBuilder ( ) ; FtpWebRequest bRequestFtp = null ; WebResponse bResponseFtp = null ; StreamReader bReadStem = null ; try { bRequestFtp = ( FtpWebRequest) FtpWebRequest. Create ( new Uri ( bFTPFolderPath) ) ; bRequestFtp. Credentials = FNetworkCredential; bRequestFtp. UseBinary = true ; bRequestFtp. Method = WebRequestMethods. Ftp. ListDirectory; bResponseFtp = bRequestFtp. GetResponse ( ) ; bReadStem = new StreamReader ( bResponseFtp. GetResponseStream ( ) , System. Text. Encoding. Default) ; string bReadStrLine = bReadStem. ReadLine ( ) ; while ( bReadStrLine != null ) { bReadStr. Append ( bReadStrLine) ; bReadStr. Append ( "\n" ) ; bReadStrLine = bReadStem. ReadLine ( ) ; } bReadStrLine = null ; if ( bReadStr. ToString ( ) . Trim ( ) != "" ) { bReadStr. Remove ( bReadStr. ToString ( ) . LastIndexOf ( '\n' ) , 1 ) ; bFolderPathFiles = bReadStr. ToString ( ) . Split ( '\n' ) ; } return bFolderPathFiles; } catch ( Exception e) { return bFolderPathFiles; } finally { bReadStem?. Close ( ) ; bReadStem = null ; bResponseFtp?. Close ( ) ; bResponseFtp = null ; bRequestFtp?. Abort ( ) ; bRequestFtp = null ; bFTPFolderPath = null ; } } public int DoFTPUpLoadFiles ( string aLocalDirPath, string aFolderName, string aFileName, string aExtension ) { int bResult = - 1 ; int bWaitTime = 0 ; string bLocalDirPath = aLocalDirPath + "\\" + aFileName + aExtension; while ( ! File. Exists ( bLocalDirPath) && bWaitTime < 100 ) { Thread. Sleep ( 100 ) ; bWaitTime++ ; } if ( ! File. Exists ( bLocalDirPath) ) { bResult = - 1 ; } else { bResult = DoFtpUploadFile ( bLocalDirPath, aFolderName, aFileName + aExtension) ; } return bResult; } private int DoFtpUploadFile ( string aLocalDirPath, string aFolderName, string aFileName ) { int bResult = - 1 ; bResult = DoFtpMakeDir ( aFolderName) ; if ( bResult != 0 ) { return bResult; } string bFTPFileFullPath = FFTPUrl + aFolderName + "/" + aFileName; FileInfo bFileInfo = null ; FileStream bFileStem = null ; Uri bUri = new Uri ( bFTPFileFullPath) ; FtpWebRequest bFTPRequest = null ; try { bFileInfo = new FileInfo ( aLocalDirPath) ; bFileStem = bFileInfo. OpenRead ( ) ; long length = bFileStem. Length; bFTPRequest = ( FtpWebRequest) WebRequest. Create ( bUri) ; bFTPRequest. Credentials = FNetworkCredential; bFTPRequest. Method = WebRequestMethods. Ftp. UploadFile; bFTPRequest. ContentLength = length; bFTPRequest. Timeout = 5 * 1000 ; Stream bStem = bFTPRequest. GetRequestStream ( ) ; int BufferLength = 2048 ; byte[ ] bBytes = new byte [ BufferLength] ; int i; while ( ( i = bFileStem. Read ( bBytes, 0 , BufferLength) ) > 0 ) { bStem. Write ( bBytes, 0 , i) ; } bStem. Close ( ) ; bStem. Dispose ( ) ; bStem = null ; bBytes = null ; bResult = 0 ; return bResult; } catch ( Exception e) { bResult = - 1 ; return bResult; } finally { bFileStem?. Dispose ( ) ; bFileStem = null ; bFileInfo = null ; bUri = null ; bFileStem?. Close ( ) ; bFileStem = null ; bFTPRequest?. Abort ( ) ; bFTPRequest = null ; bFTPFileFullPath = null ; } } public Stream DoDTPDownloadFile ( string aFileName ) { Stream bStem = null ; FtpWebResponse bFTPResponse = null ; FtpWebRequest bFTPRequest = null ; try { aFileName = aFileName. Replace ( "\\" , "/" ) ; string aUrl = FFTPUrl + aFileName; bFTPRequest = ( FtpWebRequest) FtpWebRequest. Create ( new Uri ( aUrl) ) ; bFTPRequest. UseBinary = true ; bFTPRequest. Credentials = FNetworkCredential; bFTPResponse = ( FtpWebResponse) bFTPRequest. GetResponse ( ) ; bStem = bFTPResponse. GetResponseStream ( ) ; return bStem; } catch ( Exception e) { bStem = null ; return bStem; } finally { } } int DoFtpMakeDir ( string aFolderPath ) { int bResult = - 1 ; string bFolderPath = FFTPUrl + aFolderPath; FtpWebRequest bFTPRequest = null ; FtpWebResponse bFTPResponse = null ; try { bFTPRequest = ( FtpWebRequest) FtpWebRequest. Create ( bFolderPath) ; bFTPRequest. Credentials = FNetworkCredential; bFTPRequest. Method = WebRequestMethods. Ftp. MakeDirectory; bFTPRequest. UseBinary = true ; bFTPRequest. Proxy = null ; bFTPResponse = ( FtpWebResponse) bFTPRequest. GetResponse ( ) ; bFTPResponse. Close ( ) ; bFTPResponse = null ; bResult = 0 ; return bResult; } catch ( Exception e) { if ( e. Message. Contains ( "(550) 文件不可用" ) ) { bResult = 0 ; } else { bResult = - 1 ; } return bResult; } finally { bFTPResponse?. Close ( ) ; bFTPResponse = null ; bFTPRequest?. Abort ( ) ; bFTPRequest = null ; } } public bool DoFtpDeleteDir ( string aFolderPath ) { FtpWebRequest bFTPRequest = null ; FtpWebResponse bFTPResponse = null ; try { if ( string. IsNullOrWhiteSpace ( aFolderPath) ) return true ; bFTPRequest = ( FtpWebRequest) FtpWebRequest. Create ( aFolderPath) ; bFTPRequest. Credentials = FNetworkCredential; bFTPRequest. KeepAlive = false ; bFTPRequest. ContentLength = 1000 ; bFTPRequest. Timeout = 5 * 1000 ; bFTPRequest. Method = WebRequestMethods. Ftp. DeleteFile; bFTPResponse = ( FtpWebResponse) bFTPRequest. GetResponse ( ) ; return true ; } catch ( Exception e) { return false ; } finally { bFTPResponse?. Close ( ) ; bFTPResponse = null ; bFTPRequest?. Abort ( ) ; bFTPRequest = null ; } } }
2 WebApi接口实现
[ HttpGet] public IHttpActionResult GetFTPPdfBaseStr ( string PdfName ) { try { TFTPOperationUnit bFTPOperationUnit = new TFTPOperationUnit ( "ftp://192.168.1.106:21/" , "ftp" , "123456" ) ; string base64Str = default ( string) ; int bufferSize = 2048 ; byte[ ] buffer = new byte [ bufferSize] ; using ( System. IO . Stream fileStream = bFTPOperationUnit. DoDTPDownloadFile ( PdfName) ) { int readCount; readCount = fileStream. Read ( buffer, 0 , bufferSize) ; byte[ ] bTmp = new byte [ 0 ] ; while ( readCount > 0 ) { bTmp = bTmp. Concat ( buffer) . ToArray ( ) ; readCount = fileStream. Read ( buffer, 0 , bufferSize) ; } base64Str = Convert. ToBase64String ( bTmp) ; } return Json ( new { Code = 0 , Msg = "查询成功" , Data = base64Str } ) ; } catch ( Exception e) { return Json ( new { Code = - 1 , Msg = "查询失败:" + e. Message } ) ; } }
3 MVC后台保存pdf文件
[ HttpPost] public string Base64StringToFile ( string base64String, string folder, string pdfName ) { try { string bPath = AppDomain. CurrentDomain. SetupInformation. ApplicationBase + "\\PDFfiles" ; string bFileName = pdfName + ".pdf" ; string fileFullPath = bPath + "\\" + folder; if ( ! Directory. Exists ( fileFullPath) ) { Directory. CreateDirectory ( fileFullPath) ; } if ( System. IO . File. Exists ( fileFullPath + "\\" + bFileName) ) { System. IO . File. Delete ( fileFullPath + "\\" + bFileName) ; } string strbase64 = base64String; int bufferSize = 2048 ; MemoryStream stream = new MemoryStream ( Convert. FromBase64String ( strbase64) ) ; FileStream fs = new FileStream ( fileFullPath + "\\" + bFileName, FileMode. OpenOrCreate, FileAccess. Write) ; byte[ ] bData = Convert. FromBase64String ( strbase64) ; int readCount = stream. Read ( bData, 0 , bufferSize) ; while ( readCount > 0 ) { fs. Write ( bData, 0 , readCount) ; readCount = stream. Read ( bData, 0 , bufferSize) ; } fs. Close ( ) ; fs = null ; return folder + "\\" + bFileName; } catch ( Exception e) { return "" ; } } private static string logPath = AppDomain. CurrentDomain. SetupInformation. ApplicationBase + "\\PDFfiles" ; [ HttpPost] public string Base64StringToFile ( string base64String, string folder, string pdfName ) { try { string fileName = pdfName+ ".jpg" ; string fileFullPath = logPath+ "\\" + folder; if ( ! Directory. Exists ( fileFullPath) ) { Directory. CreateDirectory ( fileFullPath) ; } if ( System. IO . File. Exists ( fileFullPath + "\\" + fileName) ) { System. IO . File. Delete ( fileFullPath + "\\" + fileName) ; } string strbase64 = base64String; MemoryStream stream = new MemoryStream ( Convert. FromBase64String ( strbase64) ) ; FileStream fs = new FileStream ( fileFullPath + "\\" + fileName, FileMode. OpenOrCreate, FileAccess. Write) ; byte[ ] b = stream. ToArray ( ) ; fs. Write ( b, 0 , b. Length) ; fs. Close ( ) ; return folder + "\\" + fileName; } catch ( Exception e) { return "" ; } }