1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Net; using System.Text; namespace ZB.QueueSys.Common { /// <summary> /// FTP帮助类,目前仅支持上传文件 /// </summary> public class FTPHelper { private static FTPHelper instance; public static FTPHelper Instance { get { if (instance == null ) instance = new FTPHelper(PubVariable.FtpServerIP, PubVariable.FtpServerPort, PubVariable.FtpServerUsername, PubVariable.FtpServerPassword); return FTPHelper.instance; } } #region 字段构造 string FtpURI; string FtpUserID; string FtpServerIP; string FtpServerPort; string FtpPassword; /// <summary> /// 连接FTP服务器 /// </summary> /// <param name="ftpServerIP">FTP连接地址</param> /// <param name="ftpServerPort">FTP服务端口</param> /// <param name="ftpUserID">用户名</param> /// <param name="ftpPassword">密码</param> public FTPHelper( string ftpServerIP, string ftpServerPort, string ftpUserID, string ftpPassword) { this .FtpServerIP = ftpServerIP; this .FtpServerPort = ftpServerPort; this .FtpUserID = ftpUserID; this .FtpPassword = ftpPassword; if (! string .IsNullOrWhiteSpace(ftpServerPort)) this .FtpURI = "ftp://" + FtpServerIP + ":" + FtpServerPort + "/" ; else this .FtpURI = "ftp://" + FtpServerIP + "/" ; } #endregion #region 上传 /// <summary> /// 上传文件 /// </summary> /// <param name="localFilePath">本地文件路径</param> /// <param name="ftpSubPath">ftp端子目录(不包含ftp://ip/信息,可包含文件名信息)。\n如"abc/123"或者"abc/123.test.jpg"</param> public bool UploadFile( string localFilePath, string ftpSubPath) { FileInfo fileInf = new FileInfo(localFilePath); if (!fileInf.Exists) return false ; WebClient client = new WebClient(); //上传改用WebClient方式 client.Credentials = new NetworkCredential(FtpUserID, FtpPassword); client.Proxy = null ; try { string desPath = "" ; if ( string .IsNullOrWhiteSpace(ftpSubPath)) desPath = FtpURI + fileInf.Name; else if (ftpSubPath.LastIndexOf( '.' ) > ftpSubPath.LastIndexOf( '/' )) //ftpSubPath为abc/test.jpg的格式 desPath = FtpURI + ftpSubPath; else desPath = FtpURI + ftpSubPath + "/" + fileInf.Name; CheckAndCreateFtpDir(ftpSubPath); client.UploadFile(desPath, localFilePath); } catch (Exception ex) { LogHelper.Default.SaveText( "FTPHelper UploadFile:" + ex.Message + ex.StackTrace); return false ; } return true ; } /// <summary> /// 上传byte[]到服务器指定文件 /// </summary> /// <param name="localFileBytes">要上传的数据内容(byte[]类型)</param> /// <param name="ftpSubPath">ftp端子目录,必须包含文件名,但不包含ftp://ip/信息,如abc/123/test.jpg</param> public bool UploadData( byte [] localFileBytes, string ftpSubPath) { WebClient client = new WebClient(); //上传改用WebClient方式 client.Credentials = new NetworkCredential(FtpUserID, FtpPassword); client.Proxy = null ; try { string desPath = "" ; if ( string .IsNullOrWhiteSpace(ftpSubPath)) return false ; else desPath = FtpURI + ftpSubPath; CheckAndCreateFtpDir(ftpSubPath); client.UploadData(desPath, localFileBytes); } catch (Exception ex) { LogHelper.Default.SaveText( "FTPHelper UploadData:" + ex.Message + ex.StackTrace); return false ; } return true ; } /// <summary> /// 上传Bitmap到服务器指定文件 /// </summary> /// <param name="bitmap">要上传的数据内容(bitmap类型)</param> /// <param name="ftpSubPath">ftp端子目录,必须包含文件名,但不包含ftp://ip/信息,如abc/123/test.jpg</param> public bool UploadData(Bitmap bitmap, string ftpSubPath) { if (bitmap == null ) return false ; return UploadData(BitmapToBytes(bitmap), ftpSubPath); } /// <summary> /// 将Bitmap转换为byte[] /// </summary> /// <param name="bitmap"></param> /// <returns></returns> private byte [] BitmapToBytes(Bitmap bitmap) { using (MemoryStream stream = new MemoryStream()) { bitmap.Save(stream, ImageFormat.Jpeg); byte [] data = new byte [stream.Length]; stream.Seek(0, SeekOrigin.Begin); stream.Read(data, 0, Convert.ToInt32(stream.Length)); return data; } } #endregion #region 目录检测并创建 /// <summary> /// 检查ftp子目录ftpDestFilePath中的各个子目录是否存在,不存在则创建 /// </summary> /// <param name="ftpDestFilePath">ftp目录中的子目录</param> private void CheckAndCreateFtpDir( string ftpDestFilePath) { string dirStr = ftpDestFilePath; if (ftpDestFilePath.LastIndexOf( '.' ) > ftpDestFilePath.LastIndexOf( '/' )) dirStr = ftpDestFilePath.Substring(0, ftpDestFilePath.LastIndexOf( '/' )); //这种情况带文件名,如"abc/123/test.txt" string [] subDirs = dirStr.Split( '/' ); string curDir = "" ; for ( int i = 0; i < subDirs.Length; i++) { string subDir = subDirs[i]; curDir += subDir + "/" ; if (!DirectoryIsExist(curDir)) MakeFTPDir(curDir); } } /// <summary> /// 检测ftp目录下的subDir子目录是否存在 /// </summary> /// <param name="subDir"></param> /// <returns>false不存在,true存在</returns> public bool DirectoryIsExist( string subDir) { FtpWebRequest reqFTP; try { reqFTP = (FtpWebRequest)FtpWebRequest.Create( new Uri(FtpURI + subDir)); reqFTP.UseBinary = true ; reqFTP.Proxy = null ; reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword); reqFTP.Method = WebRequestMethods.Ftp.ListDirectory; FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string line = reader.ReadLine(); reader.Close(); response.Close(); var result = line != null ; return result; } catch (Exception ex) { LogHelper.Default.SaveText( "FTPHelper DirectoryIsExist:" + ex.Message + ex.StackTrace); return false ; } } /// <summary> /// 创建ftp子文件夹 /// </summary> public bool MakeFTPDir( string dirName) { FtpWebRequest reqFTP; try { reqFTP = (FtpWebRequest)FtpWebRequest.Create( new Uri(FtpURI + dirName)); reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory; reqFTP.UseBinary = true ; reqFTP.Proxy = null ; reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword); FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); Stream ftpStream = response.GetResponseStream(); ftpStream.Close(); response.Close(); return true ; } catch (Exception ex) { LogHelper.Default.SaveText( "FTPHelper MakeFTPDir:" + ex.Message + ex.StackTrace); return false ; } } #endregion } } |
博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
2016-10-28 Winfrom Wait 实现转圈等待 this.Cursor = Cursors.WaitCursor;