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. 指向NULL的类

    引出:写个类A,声明类A指针指向NULL,调用类A的方法会有什么后果,编译通过吗,运行会通过吗? (在VS2008与VC++的情况下) 有错误欢迎批评指正! #include<stdio.h&g ...

  2. OneProxy常用参数说明

    5.2.OneProxy常用参数说明 OneProxy的所有可用参数可通过oneproxy --help-all查看.所有参数均可以写入文件中,由OneProxy启动时加载 5.2.1.基本参数 -- ...

  3. PAT 甲级 1011 World Cup Betting (20)(20 分)(水题,不用特别在乎精度)

    1011 World Cup Betting (20)(20 分) With the 2010 FIFA World Cup running, football fans the world over ...

  4. HDU 1251 统计难题(字典树)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  5. suse 安装gcc

    1)挂载ISO镜像 新建一个目录: mkdir /mnt/iso 将ISO文件挂载到该目录上: mount -o loop /opt/SLES-11-SP3-DVD-x86_64-GM-DVD1.is ...

  6. Lucene 中的Tokenizer, TokenFilter学习

      lucene中的TokenStream,TokenFilter之间关系   TokenStream是一个能够在被调用后产生语汇单元序列的类,其中有两个类型:Tokenizer和TokenFilte ...

  7. 在window的IIS中搭配Php的简单方法

    在window的IIS中搭配Php的简单方法.搭配php的时候找到的一个超级简单方法 关键的核心是 PHP Manager for IIS 这是微软开发的一个项目,使用它可以在window下最方便简单 ...

  8. 简单的自动化测试模型(python+selenium)

             刚接触自动化测试,由于没有编程语言的基础,是搞不懂代码里面的函数.封装.包以及其他概念,只是了解字符串.数组.元组及字典这种最基本的名词,更不懂自动化测试框架了.          ...

  9. Django之路 - 实现登录随机验证码

    登录验证码是每个网站登录时的基本标配,网上也有很多相应的文章, 但是从生成验证码到 应用到自己的网站上的全步骤,并没有看到很多, 为了节约大家的时间,我把整体步骤写下来, 即拿即用哈 1. 生成随机验 ...

  10. 关于TP5中的依赖注入和容器和facade

    看了不少的文章,也看了官方的介绍,还是根据自己的理解,写写看法,理清下思路 只是单纯的说依赖注入Dependency Injection和容器 别的不白扯 比如有A,B,C三个类 A类的1方法依赖B类 ...