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. PDO exec 执行时出错后如果修改数据会被还原?

    PDO exec 执行时出错后如果修改数据会被还原? 现象 FastAdmin 更新了 1127 版本,但是使用在线安装方式出现无法修改管理员密码的问题. 一直是默认的 admin 123456 密码 ...

  2. 完全卸载vs2013、vs2015的方法

    Visual Studio安装过程会安装好多组件,如果想要卸载的话会出现一些因难,在控制面板不容易卸载干净,在Linux下的命令都有--help参数来显示命令的用法,今天突发奇想,在控制台下输入vs2 ...

  3. vim配置之tagbar

    vimConfig/plugin/tagbar-setting.vim let g:tagbar_width=4 map <F12> :TagbarToggle<CR> map ...

  4. emacs之配置symbol浏览界面

    由于ecb的method-buffer不能更新,因此抛弃ecb,speedbar的method也不能更新,换imenu-tree 使用el-get install安装imenu-tree imenu- ...

  5. 【ZZ】大型数据库应用解决方案总结 | 菜鸟教程

    大型数据库应用解决方案总结 http://www.runoob.com/w3cnote/db-solutions.html

  6. pythonNet08

    线程通信 通信方法:多个线程共用进程空间,所以进程的全局变量对进程内线程均可见.线程往往使用全局变量进行通信 注意事项:线程间使用全局变量进行通信,全局变量为共享资源,往往需要同步互斥机制 线程的同步 ...

  7. Unix socket的准备(一)

    套接字地址结构 套接字编程中,五元组是广为人知的. (host_ip, host_port, target_ip, target_port, protocol). 其中 ip 和 port 就是由套接 ...

  8. 3_bootsrap布局容器

    3.布局容器 BootStrap必须需要至少一个布局容器,才能为页面内容进行封装和方便的样式控制. 相当于一个画板. 帮助手册位置:全局CSS样式------->概览------->布局容 ...

  9. leetcode172

    public class Solution { public int TrailingZeroes(int n) { ) { ; } else { ; var y = TrailingZeroes(x ...

  10. 使用phpexcel导出到xls文件的时候出现乱码解决

    <?php include 'global.php'; $ids = $_GET['ids']; $sql = "select * from crm_cost_end where id ...