Android部分:

              AsyncHttpClient client = new AsyncHttpClient();                    RequestParams requestParams = new RequestParams();                    File file = new File("/sdcard/DCIM/Camera/IMG_20140322_180445.jpg");                    try {                        requestParams.put("file", file);                        requestParams.put("userid", "张三");                    } catch (FileNotFoundException e) {                        Log.d("T", e.getMessage());                    }                    client.post("http://192.168.30.30:9178/?fn=upload", requestParams,                            new AsyncHttpResponseHandler() {                                @Override                                public void onSuccess(String response) {                                    Log.d("T", response);                                }                                @Override                                public void onFailure(Throwable error, String content) {                                    Log.d("T", content);                                    super.onFailure(error, content);                                }                            });
View Code

服务端部分:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using HttpServer.HttpModules;using System.Net;using System.IO;using System.Threading;using HttpServer.FormDecoders;using System.Collections.Concurrent;namespace TestHttpServer{    public partial class Form1 : Form    {        private HttpServer.HttpServer _server = null;        public Form1()        {            InitializeComponent();            Console.WriteLine("UI:" + Thread.CurrentThread.ManagedThreadId);        }        private void button1_Click(object sender, EventArgs e)        {            _server = new HttpServer.HttpServer();            _server.Add(new FileUpHttpModule());            //_server.Add(new NotHandleModule());            _server.Start(IPAddress.Any, int.Parse(textBox1.Text));            Console.WriteLine("开启服务!");        }        protected override void OnClosed(EventArgs e)        {            UploadFileMonitor.Instance.OnFileUpload -= this.DisplayPic;            if (_server != null)            {                _server.Stop();                Console.WriteLine("关闭服务!");            }            base.OnClosed(e);        }        private void button2_Click(object sender, EventArgs e)        {            new frmClient().Show();        }        private void DisplayPic(string filename)        {            if (this.InvokeRequired)            {                Console.WriteLine("需要异步调用!");                this.Invoke( (Action) (() => { DisplayPic(filename); }));            }            else            {                pictureBox1.ImageLocation = filename;                //pictureBox1.Refresh();            }        }        private void Form1_Load(object sender, EventArgs e)        {            UploadFileMonitor.Instance.OnFileUpload += this.DisplayPic;        }    }    #region 上传监控    public class UploadFileMonitor    {        const int C_MaxQueueLength = 100;        public delegate void OnFileUploadHandler(string filename);        private ConcurrentQueue<String> _Queue = new ConcurrentQueue<string>();        public event OnFileUploadHandler OnFileUpload;        private static readonly UploadFileMonitor _Instacne = new UploadFileMonitor();        public static UploadFileMonitor Instance        {            get { return _Instacne; }        }        private UploadFileMonitor() { }        public void Add(string filename)        {            _Queue.Enqueue(filename);            while (_Queue.Count > C_MaxQueueLength +10)            {                String retrive = "";                _Queue.TryDequeue(out retrive);            }            if (OnFileUpload != null)            {                try                {                    OnFileUpload(filename);                }                catch { }            }        }    }    #endregion    public class FileUpHttpModule : HttpModule    {        public override bool Process(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session)        {            try            {                Console.WriteLine("work:" + Thread.CurrentThread.ManagedThreadId);                var it = request.QueryString["fn"];                Console.WriteLine("userId:" + request.Form["userid"].Value);                Console.WriteLine(it);                String filename = "";                foreach (var file in request.Form.Files)                {                    filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.Ticks + Path.GetExtension(file.UploadFilename));                    File.Copy(file.Filename, filename, true);                    UploadFileMonitor.Instance.Add(filename);                }                StreamWriter writer = new StreamWriter(response.Body);                if (string.IsNullOrWhiteSpace(filename))                {                    writer.WriteLine("no upload file!");                }                else                {                    writer.WriteLine(filename);                }                writer.Flush();                        }            catch (Exception ex)            {                StreamWriter writer = new StreamWriter(response.Body);                writer.WriteLine(ex.Message);                writer.Flush();                        }                      return true;                    }    }}
View Code

更多相关文章

  1. android的service中在后台弹出提示框
  2. android textview部分字体变颜色
  3. Android如何从服务器获取图片
  4. Android(安卓)客户端将位置信息发送给服务端
  5. Android(安卓)Service总结06 之AIDL
  6. Android(安卓)Service总结06 之AIDL
  7. Android(安卓)service的开启和绑定,以及调用service的方法
  8. Android(安卓)保活后台启动Service 8.0踩坑记录
  9. android的service

随机推荐

  1. Android修行之路——Android程序设计基础
  2. Android 设备+APP+号码信息
  3. Android开发小记之一
  4. android shape总结 和控制的风格定制
  5. 互联网公司Android客户端面试 知识点
  6. android(EditText)属性详解
  7. Android NDK开发:SeetaFace2实现人脸匹配
  8. Android 开发——'Android Pre Compiler'
  9. 2.高焕堂讲解 ContentProvider范例
  10. android使用http协议上传文件