解决方案:

1. 在目标服务器上发布webservice,实现文件下载的方法。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Services;
  6. using System.IO;
  7. using System.Data;
  8.  
  9. namespace TRNWebService
  10. {
  11. /// <summary>
  12. /// FileHelperService 的摘要说明
  13. /// </summary>
  14. [WebService(Namespace = "http://tempuri.org/")]
  15. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  16. [System.ComponentModel.ToolboxItem(false)]
  17. // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
  18. // [System.Web.Script.Services.ScriptService]
  19. public class FileHelperService : System.Web.Services.WebService
  20. {
  21.  
  22. /// <summary>
  23. /// 从指定文件夹下获取文件
  24. /// </summary>
  25. /// <param name="dicpath"></param>
  26. /// <returns></returns>
  27. [WebMethod(EnableSession = true, Description = "获取文件夹下面的文件")]
  28. public DataTable GetFileList(string dicpath)
  29. {
  30. DataTable dt = new DataTable();
  31. dt.TableName = "AEFiles";
  32. dt.Columns.Add("fileName", typeof(string));
  33. dt.Columns.Add("filePath", typeof(string));
  34.  
  35. DirectoryInfo dir = System.IO.Directory.CreateDirectory(dicpath);
  36. if (dir == null)
  37. {
  38. return null;
  39. }
  40. FileInfo[] files = dir.GetFiles();
  41. for (int i = ; i < files.Length; i++)
  42. {
  43. FileInfo file = files[i] as FileInfo;
  44. if (file != null)
  45. {
  46. dt.Rows.Add(file.Name, file.FullName);
  47. }
  48. }
  49.  
  50. return dt;
  51.  
  52. }
  53.  
  54. [WebMethod(Description = "下载服务器站点文件,传递文件相对路径")]
  55. public byte[] DownloadFile(string strFilePath)
  56. {
  57. FileStream fs = null;
  58. //string CurrentUploadFolderPath = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["UploadFileFolder"]);
  59. //string CurrentUploadFilePath = CurrentUploadFolderPath + strFilePath;
  60. string CurrentUploadFilePath = strFilePath;
  61. if (File.Exists(CurrentUploadFilePath))
  62. {
  63. try
  64. {
  65. ///打开现有文件以进行读取。
  66. fs = File.OpenRead(CurrentUploadFilePath);
  67. int b1;
  68. System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
  69. while ((b1 = fs.ReadByte()) != -)
  70. {
  71. tempStream.WriteByte(((byte)b1));
  72. }
  73. return tempStream.ToArray();
  74. }
  75. catch (Exception ex)
  76. {
  77. return new byte[];
  78. }
  79. finally
  80. {
  81. fs.Close();
  82. }
  83. }
  84. else
  85. {
  86. return new byte[];
  87. }
  88. }
  89. }
  90. }

webservice 下载文件

2. 客户端利用反射实现webservice,把文件下载到本地的安装目录

3. 客户端打开已经存放在本地的文件

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.CodeDom.Compiler;
  6. using System.Net;
  7. using System.IO;
  8. using Microsoft.CSharp;
  9. using System.CodeDom;
  10. using System.Web.Services.Description;
  11.  
  12. namespace Frm.Common
  13. {
  14. public static class WebServiceInvoke
  15. {
  16. #region InvokeWebService
  17. //动态调用web服务
  18. public static object InvokeWebService(string url, string methodname, object[] args)
  19. {
  20. return InvokeWebService(url, null, methodname, args);
  21. }
  22.  
  23. public static object InvokeWebService(string url, string classname, string methodname, object[] args)
  24. {
  25. string @namespace = "TRNWebService";
  26. if ((classname == null) || (classname == ""))
  27. {
  28. classname = GetWsClassName(url);
  29. }
  30.  
  31. try
  32. {
  33. //获取WSDL
  34. WebClient wc = new WebClient();
  35. Stream stream = wc.OpenRead(url + "?WSDL");
  36. ServiceDescription sd = ServiceDescription.Read(stream);
  37. ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
  38. sdi.AddServiceDescription(sd, "", "");
  39. CodeNamespace cn = new CodeNamespace(@namespace);
  40.  
  41. //生成客户端代理类代码
  42. CodeCompileUnit ccu = new CodeCompileUnit();
  43. ccu.Namespaces.Add(cn);
  44. sdi.Import(cn, ccu);
  45. CSharpCodeProvider csc = new CSharpCodeProvider();
  46. ICodeCompiler icc = csc.CreateCompiler();
  47.  
  48. //设定编译参数
  49. CompilerParameters cplist = new CompilerParameters();
  50. cplist.GenerateExecutable = false;
  51. cplist.GenerateInMemory = true;
  52. cplist.ReferencedAssemblies.Add("System.dll");
  53. cplist.ReferencedAssemblies.Add("System.XML.dll");
  54. cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
  55. cplist.ReferencedAssemblies.Add("System.Data.dll");
  56.  
  57. //编译代理类
  58. CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
  59. if (true == cr.Errors.HasErrors)
  60. {
  61. System.Text.StringBuilder sb = new System.Text.StringBuilder();
  62. foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
  63. {
  64. sb.Append(ce.ToString());
  65. sb.Append(System.Environment.NewLine);
  66. }
  67. throw new Exception(sb.ToString());
  68. }
  69.  
  70. //生成代理实例,并调用方法
  71. System.Reflection.Assembly assembly = cr.CompiledAssembly;
  72. Type t = assembly.GetType(@namespace + "." + classname, true, true);
  73. object obj = Activator.CreateInstance(t);
  74. System.Reflection.MethodInfo mi = t.GetMethod(methodname);
  75.  
  76. return mi.Invoke(obj, args);
  77. }
  78. catch (Exception ex)
  79. {
  80. throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));
  81. }
  82. }
  83.  
  84. private static string GetWsClassName(string wsUrl)
  85. {
  86. string[] parts = wsUrl.Split('/');
  87. string[] pps = parts[parts.Length - ].Split('.');
  88.  
  89. return pps[];
  90. }
  91. #endregion
  92. }
  93. }

c# 利用反射实现动态webservice

  1. /// <summary>
  2. /// 通过WebService下载文件
  3. /// </summary>
  4. /// <param name="ServiceFilePath">服务器图片路径</param>
  5. /// <param name="DownloadFolderPath">本地图片路径</param>
  6. private string DownloadFile(string ServiceFilePath, string DownloadFolderPath)
  7. {
  8. try
  9. {
  10. string DownloadFileName = "";
  11. if (ServiceFilePath.Contains("/"))
  12. {
  13. DownloadFileName = ServiceFilePath.Substring(ServiceFilePath.LastIndexOf("/"));
  14. }
  15. else if (ServiceFilePath.Contains("\\"))
  16. {
  17. DownloadFileName = ServiceFilePath.Substring(ServiceFilePath.LastIndexOf("\\")+);
  18. }
  19. else
  20. {
  21. DownloadFileName = ServiceFilePath;
  22. }
  23.  
  24. string DownloadFilePath = DownloadFolderPath + "\\" + DownloadFileName;
  25.  
  26. Dictionary<string, string> dicSystem = Common.AppConfigSys.AppConfigDic;
  27. string strUrl = dicSystem["webserviceURL"];//webservice的发布位置(http://192.168.10.3:8084/FileHelperService.asmx)
  28. object[] objPara = new object[];
  29. objPara[] = ServiceFilePath;
  30. object objFile = Frm.Common.WebServiceInvoke.InvokeWebService(strUrl, "DownloadFile", objPara);
  31.  
  32. byte[] bytes = (byte[])objFile;
  33. if (bytes != null)
  34. {
  35. if (!Directory.Exists(DownloadFolderPath))
  36. {
  37. Directory.CreateDirectory(DownloadFolderPath);
  38. }
  39. if (!File.Exists(DownloadFilePath))
  40. {
  41. File.Create(DownloadFilePath).Dispose();
  42. }
  43. //如果不存在完整的上传路径就创建
  44. FileInfo downloadInfo = new FileInfo(DownloadFilePath);
  45. if (downloadInfo.IsReadOnly) { downloadInfo.IsReadOnly = false; }
  46. //定义并实例化一个内存流,以存放提交上来的字节数组。
  47. MemoryStream ms = new MemoryStream(bytes);
  48. //定义实际文件对象,保存上载的文件。
  49. FileStream fs = new FileStream(DownloadFilePath, FileMode.Create);
  50. ///把内内存里的数据写入物理文件
  51. ms.WriteTo(fs);
  52. fs.Flush();
  53. ms.Flush();
  54. ms.Close();
  55. fs.Close();
  56. fs = null;
  57. ms = null;
  58. }
  59. return DownloadFilePath;
  60. }
  61. catch (Exception ex)
  62. {
  63. return "";
  64. }
  65. }

客户端文件下载方法

  1. void AElink_Click(object sender, EventArgs e)
  2. {
  3.  
  4. LinkLabel link = sender as LinkLabel;
  5. string serverfilePath = link.Tag.ToString();//服务器上的文件地址
  6.  
  7. string clientfilePath = DownloadFile(serverfilePath, Application.StartupPath + "\\UploadFileFolder");//客户端的文件地址
  8. System.Diagnostics.Process.Start(clientfilePath);//打开本地文件
  9.  
  10. }

文件下载方法调用

winform 查看远程服务器上的文件的更多相关文章

  1. xshell终端向远程服务器上传文件方法

    centos-7下在本地终端里向远程服务器上传文件,在命令行中执行的软件. 安装命令如下: 在终端里输入如下命令: 会弹出如下窗口 选择你要上传的文件即可上传成功.

  2. Python: 如何判断远程服务器上Excel文件是否被人打开

    最近工作中需要去判断远程服务器上的某个Excel文件是否被打开,如果被人打开,则等待,如果没人打开使用,则去填写数据进Excel文件. 开始想的很简单,和其他语言一样,比如C#,打开文件,如果报错说明 ...

  3. php curl向远程服务器上传文件

    <?php /** * test.php: */ header('content-type:text/html;charset=utf8'); $ch = curl_init(); //加@符号 ...

  4. 模拟php curl向远程服务器上传文件

    test.php <?php header('content-type:text/html;charset=utf8'); $file = dirname(__FILE__).'/1.jpg'; ...

  5. Mac安装SSHFS挂载远程服务器上的文件夹到本地

    一.安装SSHFUS sshfs依赖于fuse,所以需要先安装fuse,这两个软件都可以在https://osxfuse.github.io/下载到. 注意安装顺序. 二.挂载文件夹到本地 输入一下命 ...

  6. php访问远程服务器上的文件

    test.php <?php $fp=fopen('http://www.baidu.com', 'r'); while (!feof($fp)) { $chunk=fgets($fp); ec ...

  7. 用java 代码下载Samba服务器上的文件到本地目录以及上传本地文件到Samba服务器

    引入: 在我们昨天架设好了Samba服务器上并且创建了一个 Samba 账户后,我们就迫不及待的想用JAVA去操作Samba服务器了,我们找到了一个框架叫 jcifs,可以高效的完成我们工作. 实践: ...

  8. 利用SSH在本机和远程服务器之间传输文件或文件夹

    1.从远程服务器上下载文件到本机 scp <服务器用户名>@<服务器地址>:<服务器中要下载的文件路径> <下载到本机的绝对路径> 2.从本机上传本地文 ...

  9. scp 从远程服务器上一下载文件

    scp -P202 xx3.x6.xx.xx:/usr/local/zookeeper-.zip /tmp #指定远程服务器的端口和远程服务器的目标文件 ,最后指定要下载到本的地目录 也可以从远程服务 ...

随机推荐

  1. mysql用法之创建事件

    1.创建事件:每天凌晨两点自动删除de_records表中七天以前的数据 CREATE EVENT event_delete_de_records_7days ON SCHEDULE EVERY 1 ...

  2. linux下安装oracle需要的配置

    1.检查系统包安装情况 rpm -qa|grep binutils rpm -ivh sysstat-7.0.2.rpm rpm -ivh binutils-2.17.50.0.6-14.el5.*. ...

  3. jQuery最佳实践:如何用好jQuery

    一.用对选择器 在jQuery中,你可以用多种选择器,选择同一个网页元素.每种选择器的性能是不一样的,你应该了解它们的性能差异. (1)最快的选择器:id选择器和元素标签选择器 举例来说,下面的语句性 ...

  4. 数组类型参数传递问题:$.ajax传递数组的traditional参数传递必须true

    数组类型参数传递: 若一个请求中包含多个值,如:(test.action?tid=1&tid=2&tid=3),参数都是同一个,只是指定多个值,这样请求时后台会发生解析错误,应先使用 ...

  5. http网络通信--页面源代码查看

    1.要在andorid中实现网络图片查看,涉及到用户隐私问题,所以要在AndroidManifest.xml中添加访问网络权限 <uses-permission android:name=&qu ...

  6. python 带颜色样式打印到终端

    #!/usr/bin/python # -*- coding: utf-8 -*- """ Created on Tue Aug 8 17:01:54 2017 @aut ...

  7. vue - v-text 和 v-html

    1.官方有了{{data}}绑定数据了,为啥还要v-text 因为网络问题,可以我们会卡到看“{{}}”,很尴尬吧!!! => 因此推荐用v-text 2. v-html是啥? 能吃吗 , v- ...

  8. java String->float,float->int

    类型转换代码 : String sourceStr = "0.0"; String类型 float sourceF = Float.valueOf(sourceStr); floa ...

  9. 【Statistics】CAP曲线

    功能描述 CAP曲线(Cumulative Accuracy Profile)/Power Curve(准确率/AR)是描述整个评级结果下,累计违约客户比例与累计客户比例的关系. 在完美的模型下,CA ...

  10. ECMAScript 6 | 新特性

    新特性概览 参考文章: http://www.cnblogs.com/Wayou/p/es6_new_features.html ——————————————————————————————————— ...