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);
}
});

服务端部分:

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 = ;
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 +)
{ 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; }
}
}

Android 上传文件到XP的更多相关文章

  1. android上传文件到服务器

    package com.spring.sky.image.upload.network; import java.io.DataOutputStream; import java.io.File; i ...

  2. android -上传文件到服务器

    android上传文件到服务器       重点:最好是设置好content-type这些参数的配置!     package com.spring.sky.image.upload.network; ...

  3. android 上传文件

    android对于上传文件,还是非常easy的,和java里面的上传都是一样的,基本上都是熟悉操作输出流和输入流!另一个特别重要的就是须要一些content-type这些參数的配置!  假设这些都弄好 ...

  4. Android上传文件至服务器(上)

    每一次都不能上首页,真悲催..管理员让我上一次首页? 很多时候我更愿意一个人写代码,与其在垃圾代码上改改改,我更愿意直接重构. 整洁的代码简单直接.整洁的代码如同优美的散文.整洁的代码从不隐藏设计者的 ...

  5. android上传文件到wamp服务器

    1.php server(wamp)部分 建立unload.php页面代码如下 <?php move_uploaded_file($_FILES["file1"][" ...

  6. 【经验记录】Android上传文件到服务器

    Android中实现上传文件,其实是很简单的,和在java里面是一样的,基本上都是熟悉操作输出流和输入流!还有一个特别重要的就是需要配置content-type的一些参数!如果这些都弄好了,上传就很简 ...

  7. Android上传文件到服务器(转)

    Android中实现上传文件,其实是很简单的,和在java里面是一样的,基本上都是熟悉操作输出流和输入流!还有一个特别重要的就是需要配置content-type的一些参数!如果这些都弄好了,上传就很简 ...

  8. Android上传文件之FTP

    android客户端实现FTP文件(包括图片)上传应该没什么难度.写下来就了为了记录一下,望能帮到新手. 需要用到 commons-net-3.0.1.jar,后面附上jar包. 直接上代码: /** ...

  9. Android 上传文件到 FTP 服务器

    实现背景 近期接触到一个需求,就是将文件从Android系统上传到FTP服务器,虽然之前接触过FTP服务器,了解基本的使用流程,但是将此流程从使用习惯转化为代码实现还是有一定难度的.但是基本的流程还是 ...

随机推荐

  1. 阻塞和非阻塞I/O

    阻塞和非阻塞I/O是设备访问的两种不同模式,驱动程序可以灵活的支持用户空间对设备的这两种访问形式.        阻塞操作是指在执行设备操作时,若不能获得资源,则挂起进程直到满足可操作的条件后在进行操 ...

  2. 转HTTP协议 --- Cookie

    转自:http://www.cnblogs.com/TankXiao/archive/2013/04/15/2848906.html Cookie是HTTP协议中非常重要的东西, 之前拜读了Fish ...

  3. Mysql root 用户密码忘记后重置root密码

    [windows] 1.停止mysql服务:打开命令行窗口CMD,Net stop mysql 2.用另外一种方式启动Mysql:在命令行进入到mysql的安装路径下的bin目录下使用 mysqld- ...

  4. (转)powerdesigner 生成sql脚本使用的设置

    本文转载自:http://blog.163.com/lizhihaoo@126/blog/static/103121661201036171115/ 1. 生成sql脚本的时候,提示"con ...

  5. springMVC国际化配置和使用

    下面是基于session的,springMVC国际花的一个例子: 需求是 输入url:展示中文界面 http://localhost:8080/MySSM/user?lang=zh 输入url:  展 ...

  6. expect学习笔记及用法

    expect学习笔记及实例详解 expect的基本用法 expect用法

  7. ThinkPHP实现事务回滚示例代码

    ThinkPHP的事务回滚示例如下: ? 1 2 3 4 5 6 7 8 9 10 $m=D('YourModel');//或者是M(); $m2=D('YouModel2'); $m->sta ...

  8. 简易的RPC调用框架(大神写的)

    RPC,即 Remote Procedure Call(远程过程调用),说得通俗一点就是:调用远程计算机上的服务,就像调用本地服务一样. RPC 可基于 HTTP 或 TCP 协议,Web Servi ...

  9. 代理Servlet过滤器

    Spring Security借助一些列Servlet 过滤器 来提供 各种 安全性功能. 我们只需要在应用中的 web.xml 中配置 一个过滤器. <filter> <filte ...

  10. 225. Implement Stack using Queues + 232. Implement Queue using Stacks

    ▶ 栈和队列的相互表示.发现内置的队列和栈结构都十分高效,相互表示后性能损失都很小. ▶ 第 225 题,用队列实现栈 ● 自己的代码,3 ms,单队列实现,入栈 O(1),读取栈顶元素 O(n),出 ...