///// <summary> ///// 从本地配置文件ftp_yj.ini中获取ftpIP ///// </summary> ///// <returns></returns> //public static string getFtpIP() //{ // string path = DBUtility.AppTempFilePath + "\\ftp_yj.ini"; // if (File.Exists(path)) // { // //读取文件内容 // FileStream fs = new FileStream(path, FileMode.Open); // StreamReader sr = new StreamReader(fs, Encoding.Default); // FtpServerIP = sr.ReadToEnd(); // sr.Close(); // } // return FtpServerIP; //} /// <summary> /// 获取上传地址 /// </summary> /// <param name="ftpServerIP">ftpIP</param> /// <param name="ftpServerDirectoryName">父目录</param> /// <returns>包括子目录的上传地址</returns> public static string getUploadUri() { //得到服务器一级目录的路径 string TopUploadURI = "ftp://" + FtpServerIP + "/"; //判断ftp目录信息 clsFTP cf = new clsFTP(new Uri(TopUploadURI + FtpServerDirectoryName + "/"), FtpUser, FtpPassword); string year = DateTime.Now.Year.ToString(); string month = DateTime.Now.Month.ToString(); if (month.Length == 1) { month = "0" + month; } string subDirectory = year + month; //判断子目录是否存在 if (!cf.DirectoryExist(subDirectory)) { cf.MakeDirectory(subDirectory); } string uploadURI = TopUploadURI + FtpServerDirectoryName + "/" + subDirectory + "/"; return(uploadURI); }
/// <summary> /// 删除ftp中的文件 /// </summary> /// <param name="ftpPath">ftp目录路径</param> /// <param name="remoteFileName">远程文件名</param> public static void DeleteFile(string ftpPath, string remoteFileName) { if (PublicClass.IsRegister && !PublicClass.IsSingleClent) { if (UIPublicClass.PublicClass.IsFTP || !PublicClass.strToInt(ftpPath)) { #region 从FTP删除原件 //获取ftpIP if (FtpServerIP == string.Empty) { //if (getFtpIP() == string.Empty) //{ MessageBox.Show("未能获取连接原件的配置信息\t\n\t\n删除远程文件失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; //} } if (ftpPath != string.Empty && remoteFileName != string.Empty) { string FtpPath = TopUploadURI + ftpPath; //ftp的路径 string FtpFileName = remoteFileName; //ftp中保存的文件的名称 try { clsFTP cf = new clsFTP(new Uri(FtpPath), FtpUser, FtpPassword); bool ishave = cf.FileExist(FtpFileName); if (ishave) { cf.DeleteFile(FtpFileName); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } #endregion } else { #region 从数据库删除原件 try { DeleteYj(Convert.ToInt32(ftpPath)); } catch { } #endregion } } else { #region 本地存储 if (ftpPath != string.Empty && remoteFileName != string.Empty) { //string address = DBUtility.AppTempFilePath + "\\" + ftpPath + remoteFileName; string address = CustomDirectory + "\\" + ftpPath + remoteFileName; if (File.Exists(address)) { File.Delete(address); } } #endregion } }
/// <summary> /// 下载文件 /// </summary> /// <param name="ftpPath">文件存放在ftp中的路径</param> /// <param name="remoteFileName">文件在ftp中的文件名</param> /// <param name="LocalFilePath">保存文件的本地路径,后面带有"\"</param> /// <param name="LocalFileName">保存本地的文件名</param> public static void DownLoadFile(string ftpPath, string remoteFileName, string LocalFilePath, string LocalFileName) { try { if (PublicClass.IsRegister && !PublicClass.IsSingleClent) { if (UIPublicClass.PublicClass.IsFTP || !PublicClass.strToInt(ftpPath)) { #region 从ftp下载原件 //获取ftpIP if (FtpServerIP == string.Empty) { MessageBox.Show("未能获取查看原件的配置信息\t\n\t\n查看原件失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (ftpPath != string.Empty && remoteFileName != string.Empty) { string FtpPath = TopUploadURI + ftpPath; //ftp的路径 string FtpFileName = remoteFileName; //ftp中保存的文件的名称 try { clsFTP cf = new clsFTP(new Uri(FtpPath), FtpUser, FtpPassword); //下载原价 cf.DownloadFile(FtpFileName, LocalFilePath, LocalFileName); } catch { Image img = UIPublicClass.ImagePublic.暂无图片; img.Save(LocalFilePath + LocalFileName, System.Drawing.Imaging.ImageFormat.Jpeg); } } #endregion } else if (UIPublicClass.PublicClass.IsWCF) { if (remoteFileName == string.Empty) { return; } string SaveFilePath = DBUtility.AppTempFilePath + "\\TempFile\\";//保存到本地的路径 if (!Directory.Exists(SaveFilePath)) { Directory.CreateDirectory(SaveFilePath); } else { try { Directory.Delete(SaveFilePath, true); Directory.CreateDirectory(SaveFilePath); } catch { } } //先判断本地是否有此原件,如有就直接打开 if (File.Exists(SaveFilePath + remoteFileName)) { OpenLocalFile(SaveFilePath, remoteFileName); } else { try { Stream filestream = new MemoryStream(); UIPublicClass.WcfService.DownFile inValue = new UIPublicClass.WcfService.DownFile(); inValue.FileName = remoteFileName; inValue.SubDirectory = ftpPath; var service = GetServiceProxy(); UIPublicClass.WcfService.DownFileResult retVal = service.DownLoadFile(inValue); bool issuccess = retVal.IsSuccess; string message = retVal.Message; filestream = retVal.FileStream; long filesize = retVal.FileSize; if (issuccess) { byte[] buffer = new byte[filesize]; FileStream fs = new FileStream(SaveFilePath + remoteFileName, FileMode.Create, FileAccess.Write); int count = 0; while ((count = filestream.Read(buffer, 0, buffer.Length)) > 0) { fs.Write(buffer, 0, count); } //清空缓冲区 fs.Flush(); //关闭流 fs.Close(); //MessageBox.Show("下载成功!"); } else { Image img = UIPublicClass.ImagePublic.暂无图片; img.Save(LocalFilePath + LocalFileName, System.Drawing.Imaging.ImageFormat.Jpeg); } } catch { Image img = UIPublicClass.ImagePublic.暂无图片; img.Save(LocalFilePath + LocalFileName, System.Drawing.Imaging.ImageFormat.Jpeg); } } } else { #region 从数据库下载原件 MemoryStream ms = new MemoryStream(); ms = OpenYj(Convert.ToInt32(ftpPath)); if (ms != null) { FileStream Fs = new FileStream(LocalFilePath + LocalFileName, FileMode.Create); BinaryWriter Bw = new BinaryWriter(Fs); Bw.Write(ms.ToArray()); Fs.Close(); Bw.Close(); } else { Image img = UIPublicClass.ImagePublic.暂无图片; img.Save(LocalFilePath + LocalFileName, System.Drawing.Imaging.ImageFormat.Jpeg); } #endregion } } else { #region 本地存储 if (ftpPath != string.Empty && remoteFileName != string.Empty) { string from = CustomDirectory + "\\" + ftpPath + remoteFileName; string address = LocalFilePath + LocalFileName; if (File.Exists(from)) { File.Copy(from, address, true); } else { Image img = UIPublicClass.ImagePublic.暂无图片; address = address.Substring(0, (address.Length - 4)) + ".jpg"; img.Save(address, System.Drawing.Imaging.ImageFormat.Jpeg); } } #endregion } } catch (Exception ex) { MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
/// <summary> 上传原件 </summary> /// <param name="LocalFilePath">本地文件路径(含文件名)</param> /// <param name="LocalFileName">本地文件名称</param> /// <param name="LocalFileExt">本地文件扩展名</param> /// <param name="RemoteFileName">上传后保存的文件名</param> /// <param name="UploadURI">上传路径(不包含文件名)</param> /// <returns></returns> public static bool UpLoadFile(string LocalFilePath, ref string LocalFileName, ref string LocalFileExt, ref string RemoteFileName, ref string UploadURI) { if (PublicClass.IsRegister && !PublicClass.IsSingleClent) { //本地路径和文件名称信息 LocalFileName = LocalFilePath.Substring(LocalFilePath.LastIndexOf("\\") + 1); LocalFileExt = LocalFileName.Substring(LocalFileName.LastIndexOf(".") + 1); //存放到服务器中的文件名称 //RemoteFileName = getNewFileName(LocalFileName) + "x"; RemoteFileName = getNewFileName(LocalFileName); if (UIPublicClass.PublicClass.IsFTP) { #region 至ftp //获取ftpIP if (FtpServerIP == string.Empty) { MessageBox.Show("未能获取上传原件的配置信息\t\n\t\n上传原件失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } try { UploadURI = getUploadUri(); //获取上传路径 clsFTP cf = new clsFTP(new Uri(UploadURI), FtpUser, FtpPassword); //初始化上传类 bool upload = cf.UploadFile(LocalFilePath, RemoteFileName); //上传原件 return(upload); } catch (Exception ex) { MessageBox.Show(ex.Message); return(false); } #endregion } else if (UIPublicClass.PublicClass.IsWCF) { try { UploadURI = DateTime.Now.ToString("yyyyMMdd"); using (Stream fs = new FileStream(LocalFilePath, FileMode.Open, FileAccess.Read)) { UIPublicClass.WcfService.UpFile inValue = new UIPublicClass.WcfService.UpFile(); inValue.FileName = RemoteFileName; inValue.FileSize = fs.Length; inValue.SubDirectory = UploadURI; inValue.FileStream = fs; var service = GetServiceProxy(); UIPublicClass.WcfService.UpFileResult retVal = service.UpLoadFile(inValue); if (retVal.IsSuccess) { //MessageBox.Show("上传成功!"); return(retVal.IsSuccess); } else { MessageBox.Show(retVal.Message); return(false); } } } catch (Exception ex) { MessageBox.Show(ex.Message); return(false); } } else { #region 存储至数据库 int id = CreateYj(LocalFilePath); UploadURI = TopUploadURI + id.ToString(); return((id == 0) ? false : true); #endregion } } else { #region 本地存储 //本地路径和文件名称信息 LocalFileName = LocalFilePath.Substring(LocalFilePath.LastIndexOf("\\") + 1); LocalFileExt = LocalFileName.Substring(LocalFileName.LastIndexOf(".") + 1); //存放到服务器中的文件名称 RemoteFileName = getNewFileName(LocalFileName) + "x"; //获取上传路径 UploadURI = "ArchivesYJ\\"; //string address = DBUtility.AppTempFilePath + "\\" + UploadURI; string address = CustomDirectory + "\\" + UploadURI; if (!Directory.Exists(address)) { Directory.CreateDirectory(address); } address = address + RemoteFileName; if (File.Exists(LocalFilePath)) { File.Copy(LocalFilePath, address, true); Boolean p = File.Exists(address);//查找文件是否存在 if (p) { return(true); } } else { MessageBox.Show("未能获取原件信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } return(false); #endregion } }