zoukankan      html  css  js  c++  java
  • webservice

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    using System.Xml.Linq;
    using System.IO;
    using System.Configuration;
    
    namespace UpDownFile
    {
        /// <summary>
        /// UpDownService 的摘要说明
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [ToolboxItem(false)]
        // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
        // [System.Web.Script.Services.ScriptService]
        public class UpDownService : System.Web.Services.WebService
        {
            string keyvalue = ConfigurationManager.AppSettings["SecurityKey"];
            public SecurityHeader securityKey = new SecurityHeader();
    
            //将Stream流转换为byte数组的方法。
            //PS:原本想把这个方法也当做WebMethod的,因为客户端在上传文件时也要调用该方法,后来发现Stream类型的不能通过WebService传输。。。:(
            public byte[] ConvertStreamToByteBuffer(Stream s)
            {
                MemoryStream ms = new MemoryStream();
                int b;
                while ((b = s.ReadByte()) != -1)
                {
                    ms.WriteByte((byte)b);
                }
                return ms.ToArray();
            }
    
            //上传文件至WebService所在服务器的方法,这里为了操作方法,文件都保存在UpDownFile服务所在文件夹下的File目录中
    
            [WebMethod]
            [SoapHeader("securityKey")]
            public bool Up(byte[] data, string filename)
            {
                if (securityKey.SecurityKey.Equals(keyvalue))
                {
                    try
                    {
                        if (!Directory.Exists(Server.MapPath("Images/") + filename.Substring(0, filename.LastIndexOf('\\'))))
                        {
                            Directory.CreateDirectory(Server.MapPath("Images/") + filename.Substring(0, filename.LastIndexOf('\\')));
                        }
                        FileStream fs = File.Create(Server.MapPath("Images/") + filename);
                        fs.Write(data, 0, data.Length);
                        fs.Close();
                        return true;
                    }
                    catch
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
    
            //下载WebService所在服务器上的文件的方法
    
            [WebMethod]
            [SoapHeader("securityKey")]
            public byte[] Down(string filename)
            {
                if (securityKey.SecurityKey.Equals(keyvalue))
                {
                    string filepath = Server.MapPath("Images/") + filename;
                    if (File.Exists(filepath))
                    {
                        try
                        {
                            FileStream s = File.OpenRead(filepath);
                            return ConvertStreamToByteBuffer(s);
                        }
                        catch
                        {
                            return new byte[0];
                        }
                    }
                    else
                    {
                        return new byte[0];
                    }
                }
                else
                {
                    return new byte[0];
                }
            }
        }
    }
    
  • 相关阅读:
    Python 中的map函数,filter函数,reduce函数
    编程中,static的用法详解
    C++ list容器系列功能函数详解
    python中的configparser类
    310实验室OTL问题----将写好的C++文件转换成Python文件,并将数据可视化
    310实验室OTL问题
    常量指针、指针常量、指向常量的指针常量
    Iterator迭代器的相关问题
    struts2中action中的通配符
    struts2访问servlet API
  • 原文地址:https://www.cnblogs.com/feathers/p/2103221.html
Copyright © 2011-2022 走看看