通过Web Services上传和下载图片文件

     通过Web Services上传和下载图片文件

    随着Internet技术的发展和跨平台需求的日益增加,Web Services的应用越来越广,我们不但需要通过Web Services传递字符串信息,而且需要传递二进制文件信息。下面,我就分别介绍如何通过Web Services从服务器下载文件到客户端和从客户端通过Web Services上载文件到服务器。
     我们这里建立的Web Services的名称为GetBinaryFile,提供两个公共方法:分别是GetImage()和GetImageType(),前者返回二进制文件字节数组,后者返回文件类型,其中,GetImage()方法有一个参数,用来在客户端选择要显示或下载的文件名字。这里我们所显示和下载的文件可以不在虚拟目录下,采用这个方法的好处是:可以根据权限对文件进行显示和下载控制,从下面的方法我们可以看出,实际的文件位置并没有在虚拟目录下,因此可以更好地对文件进行权限控制,这在对安全性有比较高的情况下特别有用。这个功能在以前的ASP程序中可以用Stream对象实现。为了方便读者进行测试,这里列出了全部的源代码,并在源代码里进行介绍和注释。
     

ExpandedBlockStart.gifContractedBlock.gif /**//// <summary>
InBlock.gif    
/// Web 服务提供的方法,返回给定文件的字节数组。
ExpandedBlockEnd.gif    
/// </summary>

None.gif    [WebMethod(Description="Web 服务提供的方法,返回给定文件的字节数组")]
None.gif    
public byte[] GetImage(string requestFileName)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif     
/**////得到服务器端的一个图片
ExpandedSubBlockEnd.gif     
///如果你自己测试,注意修改下面的实际物理路径

InBlock.gif     if(requestFileName == null || requestFileName == "")
InBlock.gif      
return getBinaryFile("E:\\getpic\\xp.JPG");
InBlock.gif     
else
InBlock.gif      
return getBinaryFile("E:\\getpic\\" + requestFileName);
ExpandedBlockEnd.gif    }

None.gif
ExpandedBlockStart.gifContractedBlock.gif    
/**//// <summary>
InBlock.gif    
/// getBinaryFile:返回所给文件路径的字节数组。
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="filename"></param>
ExpandedBlockEnd.gif    
/// <returns></returns>

None.gif    public byte[] getBinaryFile(string filename)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif     
if(File.Exists(filename))
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      
try
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**////打开现有文件以进行读取。
InBlock.gif       FileStream s = File.OpenRead(filename);
InBlock.gif       
return ConvertStreamToByteBuffer(s);
ExpandedSubBlockEnd.gif      }

InBlock.gif      
catch(Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif       
return new byte[0];
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif     }

InBlock.gif     
else
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      
return new byte[0];
ExpandedSubBlockEnd.gif     }

ExpandedBlockEnd.gif    }

ExpandedBlockStart.gifContractedBlock.gif  
/**//// <summary>
InBlock.gif  
/// ConvertStreamToByteBuffer:把给定的文件流转换为二进制字节数组。
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="theStream"></param>
ExpandedBlockEnd.gif  
/// <returns></returns>

None.gif    public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif     
int b1;
InBlock.gif     System.IO.MemoryStream tempStream 
= new System.IO.MemoryStream();
InBlock.gif     
while((b1=theStream.ReadByte())!=-1)
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      tempStream.WriteByte(((
byte)b1));
ExpandedSubBlockEnd.gif     }

InBlock.gif     
return tempStream.ToArray();
ExpandedBlockEnd.gif    }

None.gif     [WebMethod(Description
="Web 服务提供的方法,返回给定文件类型。")]
None.gif     
public string GetImageType()
ExpandedBlockStart.gifContractedBlock.gif     
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**////这里只是测试,您可以根据实际的文件类型进行动态输出
InBlock.gif      return "image/jpg";
ExpandedBlockEnd.gif     }

None.gif [WebMethod(Description 
= "Web 服务提供的方法,返回是否文件上载成功与否。")]
None.gif 
public string UploadFile(byte[] fs, string FileName)
ExpandedBlockStart.gifContractedBlock.gif 
dot.gif{
InBlock.gif  
try
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**////定义并实例化一个内存流,以存放提交上来的字节数组。
InBlock.gif   MemoryStream m = new MemoryStream(fs);
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**////定义实际文件对象,保存上载的文件。
InBlock.gif   FileStream f = new FileStream(Server.MapPath("."+ "\\"
InBlock.gif    
+ FileName, FileMode.Create);
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**////把内内存里的数据写入物理文件
InBlock.gif   m.WriteTo(f);
InBlock.gif   Bitmap bm 
= null;
InBlock.gif   bm 
= new Bitmap(f);
InBlock.gif   bm.Save(Server.MapPath(
"."+ "\\"
InBlock.gif    
+ FileName+".JPEG");
InBlock.gif   m.Close();
InBlock.gif   f.Close();
InBlock.gif   f 
= null;
InBlock.gif   m 
= null;
InBlock.gif   
return "文件已经上传成功。";
ExpandedSubBlockEnd.gif  }

InBlock.gif  
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
return ex.Message;
ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif }

None.gif
None.gif
None.gif}
None.gif
None.gif


客户端代码片段:

None.gifprivate void button1_Click(object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif{
InBlock.gif   pic.Service p 
= new getanddownpic.pic.Service();
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**////定义并初始化文件对象;
ExpandedSubBlockEnd.gif   
///得到二进制文件字节数组;

InBlock.gif   byte[] image = p.GetImage("");
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**////转换为支持存储区为内存的流
InBlock.gif   System.IO.MemoryStream memStream = new System.IO.MemoryStream(image);
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**////定义并实例化Bitmap对象
InBlock.gif   Bitmap bm = new Bitmap(memStream);
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**////根据不同的条件进行输出或者下载;
InBlock.gif   this.pictureBox1.Image = bm;
ExpandedBlockEnd.gif  }

None.gif
None.gif  
private void button2_Click(object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif{
InBlock.gif   openFileDialog1.InitialDirectory 
= "C:/";
InBlock.gif   openFileDialog1.Filter 
= "All Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg";
InBlock.gif   openFileDialog1.FilterIndex 
= 2;
InBlock.gif   
if (openFileDialog1.ShowDialog() == DialogResult.OK)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    pictureBox1.Image 
= Image.FromFile(openFileDialog1.FileName);
InBlock.gif    pictureBox1.SizeMode 
= PictureBoxSizeMode.CenterImage;
InBlock.gif    pictureBox1.BorderStyle 
= BorderStyle.Fixed3D;
InBlock.gif    
InBlock.gif
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif  }

None.gif
None.gif  
private void button3_Click(object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif{
InBlock.gif   Bitmap bm 
= (Bitmap)this.pictureBox1.Image;
InBlock.gif   System.IO.MemoryStream memStream 
= new System.IO.MemoryStream();
InBlock.gif   
if (this.textBox1.Text != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     bm.Save(memStream, System.Drawing.Imaging.ImageFormat.Jpeg);
InBlock.gif     
int size = (int)memStream.Length;
ExpandedSubBlockStart.gifContractedSubBlock.gif     
/**////处理上载的文件流信息。
InBlock.gif     byte[] b = new byte[size];
InBlock.gif     b 
= memStream.GetBuffer();
InBlock.gif     memStream.Close();
InBlock.gif     pic.Service up 
= new getanddownpic.pic.Service();
InBlock.gif     
string r = up.UploadFile(b, this.textBox1.Text);
InBlock.gif     MessageBox.Show(r);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     MessageBox.Show(ex.Message);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }

InBlock.gif   
else
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    MessageBox.Show(
"please input a file name");
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif  }

None.gif
None.gif

转载于:https://www.cnblogs.com/dengsu888666/archive/2006/07/22/457168.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/424136.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

300. Longest Increasing Subsequence

文章目录1 题目理解2 动态规划3 二分贪心1 题目理解 Given an integer array nums, return the length of the longest strictly increasing subsequence. A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing…

第一百一十五期:Web开发必须掌握的三个技术:Token、Cookie、Session

在Web应用中&#xff0c;HTTP请求是无状态的。即&#xff1a;用户第一次发起请求&#xff0c;与服务器建立连接并登录成功后&#xff0c;为了避免每次打开一个页面都需要登录一下&#xff0c;就出现了cookie&#xff0c;Session。 作者&#xff1a;一颗小梪梪 在Web应用中&am…

这个博客复活了

原来因为页面配置的问题出了一堆的bug,久而久之就不想管了。 但是经过了漫长的寻找博客的过程中&#xff0c;我还是回到了这里。 洛谷博客实在是有点太简陋了&#xff0c;\(hexo github\)上传太麻烦&#xff0c;要clean然后g,然后d。 之后我就重新弄了下界面&#xff0c; 现在…

第一百一十六期:不能错过!你必须知道的3种重要Python技能

学习Pandas是很棒的体验&#xff0c;学习Numpy也很有趣。但是&#xff0c;你是否过早地开始使用程序库了呢&#xff1f;这也许是因为你还没有意识到pure python的魅力。 作者&#xff1a;读芯术 学习Pandas是很棒的体验&#xff0c;学习Numpy也很有趣。但是&#xff0c;你是否…

Erlang消息传递-tut15.erl

作 w3cschool erlang 的练习 --------- 1 -module(tut15).2 3 -export([start/0, ping/2, pong/0]).4 5 %% Erlang 消息传递6 %% Erlang 进程之间的消息可以是任何简单的 Erlang 项. 比如说, 可以是列表、元组、整数、原子、进程标识等等7 %% recevie 关键字的语法: 请注意, 在…

673. Number of Longest Increasing Subsequence

文章目录1 题目理解2 动态规划1 题目理解 Given an integer array nums, return the number of longest increasing subsequences. Notice that the sequence has to be strictly increasing. 输入&#xff1a;整数数组int[] nums 输出&#xff1a;最长递增子序列的个数 规则&…

第一百一十七期:爱上 Go 语言的10个理由

这个月 Go 语言就将迎来它的10岁生日了&#xff0c;于是我们特地列出了10条让你可以开心使用 Go 语言的理由。 作者&#xff1a;4bytes 这个月 Go 语言就将迎来它的10岁生日了&#xff0c;于是我们特地列出了10条让你可以开心使用 Go 语言的理由。 Map 集合/映射默认使用0值 …

Nhibernate.hbm2ddl.auto配置详解

hibernate.cfg.xml 中hibernate.hbm2ddl.auto配置节点如下&#xff1a; <properties><property name"hibernate.show_sql" value"true" /> <property name"hibernate.hbm2ddl.auto" value"create" /></prop…

1048. Longest String Chain

文章目录1题目理解2 动态规划1题目理解 输入&#xff1a;字符串数组words&#xff0c;字符串只包含小写字母 规则&#xff1a;对于word1和word2&#xff0c;如果在word1中任何一个位置添加一个字符能够得到word2&#xff0c;那么可以称word1为word2的前身。词链是单词 [word_1…

第一百一十八期:运行 JavaScript 代码片段的 20 种工具

运行 JavaScript 代码片段的 20 种工具 前端日常开发中&#xff0c;我们使用喜爱的 IDE 调试 JavaScript 代码&#xff0c;比如我喜欢的代码编辑器有两个&#xff0c;Sublime Text 3 和 VS Code&#xff0c;前几年还使用过 Atom&#xff0c;偶尔我们会遇到临时需要快速分享给同…

如何建立好的索引.--针对Distinct

SQL语句SELECT DISTINCT EDOWN,EDOPT FROM EMFLIB.EQEDIPF WHERE EDCHK1 AND EDCHK2M AND EDCHK3 AND EDCHK4 AND EDTAG2数据库记录总数 1518741条符合查询的记录数 657225条最早的索引 EDCHK1 EDCHK2 EDCHK3 EDCHK4 EDTAG2 EDERRCODE消耗的时间:1分钟左右为什么呢?主要的原因…

一年总结

从九月份开学到现在为止&#xff0c;将近一年的学习结束&#xff0c;做一下简要总结&#xff0c;记录一下这一年的收获。这将近一年的生活&#xff08;除去上课时间&#xff09;可以用几个单词概括。吃饭->实验室->例会&#xff08;每周一次&#xff09;->看代码->…

140. Word Break II

文章目录1 题目理解2 回溯记忆化1 题目理解 140与130的区别是&#xff0c;当字符串可分的时候&#xff0c;要求返回具体的分割字符串。 2 回溯记忆化 对于字符串s&#xff0c;如果前面一部分是单词列表中的词&#xff0c;则拆分出这个单词&#xff0c;右边的继续分割。 分割…

spring mvc学习(25):Eclipse设置代码自动提示

Eclipse只需几步简单的设置就可以像idea那样代码自动提示了&#xff0c;喜欢的小伙伴可以赶紧动手设置&#xff0c;提升效率。 第一步&#xff1a;打开Eclipse --> Window --> Preferences 第二步&#xff1a;点击Java --> 打开Editor --> 点击Content Assist 第…

打破牢笼,展望更高层次的世界

打破牢笼&#xff0c;展望更高层次的世界--------------------------------------------------------------笔者袁永福是一个十多年的老程序猿&#xff0c;一穷二白的出来创业多年&#xff0c;期间经历许多曲折和磨难&#xff0c;成功的在炮火连天的商业战场上活了下来&#xf…

spring mvc学习(26):处理数据模型--从表单到controller传输数据

创建maven项目就不说了&#xff0c;需要的找我前面的博客 pom.xml文件 <project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation"http://maven.apache.org/POM/4.0.0 http…

第一课 回归问题与应用

本系列是七月算法机器学习课程笔记 文章目录1 不同类型的学习2 基本术语与概念3 线性回归模型3.1 什么是线性回归3.2 损失函数3.3 最小化损失函数-梯度下降3.4 学习率有什么影响3.5 过拟合与欠拟合4 逻辑回归4.1 为什么要有逻辑回归4.2 什么是逻辑回归4.3决策边界线性边界判定非…

[推荐] TechNet 广播 SQL Server 2000完结篇

TechNet中文网络广播在之前已经推出了SQL Server 2000的基础系列和管理专家系列&#xff0c;使广大听众认识并掌握了SQL Server 2000的管理技巧。本次系列作为前两次系列课程的完结篇&#xff0c;将会从性能调优及维护的角度为广大听众提供了一道实用而精致的大餐&#xff0c;本…

spring mvc学习(28):get乱码解决

get请求乱码情况 编写一个RegistServlet处理用户的Get请求数据 View Code 运行结果发现输入中文提交后显示结果为乱码&#xff1a; jsp页面中 <meta http-equiv"content-type" content"text/html; charsetUTF-8">通知浏览器以utf-8解码 get请求…

第二课 决策树与随机森林

本系列是七月算法机器学习课程笔记 文章目录1 从LR到决策树1.1 决策树1.2 决策树的终止条件1.3 决策树划分依据1.3.1 信息熵1.3.2 信息增益1.3.3 ID3模型1.3.4 信息增益率1.3.5 基尼指数1.3.6 信息熵与基尼指数1.3.7 连续值属性2 回归树2.1 回归树构建方法3 从决策树到随机森林…