Android部分:

Android 上传文件到XP
              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

服务端部分:

Android 上传文件到XP
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. 【Demo实例】Android FTP上传带进度条(优化后代码)
  2. Android通过http协议POST传输方式(输出流提交到服务端)
  3. android访问服务器端上传及服务器端接收 .
  4. android访问服务器端上传及服务器端接收
  5. android上传图片至服务器
  6. Android上传下载文件(图片)
  7. Android: 上传图片到服务器
  8. android通过http上传图片
  9. struts2服务端与android交互

随机推荐

  1. 深入浅出学习 Android之Android布局管理:L
  2. 【 Android(安卓)10 四大组件 】系列 --
  3. PHP 数据类型的转换与检测|变量声明方式
  4. Android开发 - 掌握ConstraintLayout(二)介
  5. android selector 背景选择器的使用, butt
  6. Android(安卓)类似美团的选择城市界面
  7. Android遇上Kotlin | Kotlin若只如初见
  8. Android(安卓)嵌套XRecyclerView滑动迟缓
  9. 在Windows7上用Android(安卓)Studio编译T
  10. Android实践 -- 监听外置sdcard(TF卡)的