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];
                }
            }
        }
    }
    
  • 相关阅读:
    Android语音识别功能使用
    Android 游戏开发必备的基础知识
    程序员需要具备的基本技能
    在IDEA上开发Android
    Eclipse中看java源代码
    Android高效开发:
    优秀程序员的十个习惯
    Android 自定义控件
    Android文件读写,保存数据
    java面试题(一)
  • 原文地址:https://www.cnblogs.com/feathers/p/2103221.html
Copyright © 2011-2022 走看看