安卓利用HTTPCLIENT向C#的ashx服务器提交一个图片

baidu_18750827 2014-08-01 03:43:16
public HttpHelp() {
myRunnable = new Runnable() {
@Override
public void run() {
myClient = new DefaultHttpClient();
Message msg = new Message();
Bundle bundle = new Bundle();
HttpResponse response = null;
HttpRequestBase myreq = null;
myreq = new HttpPost(url);
try {
//这里是把图片传到ashx里面,这种写法对吗?
MultipartEntity mulentity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
String path = Environment.getExternalStorageDirectory()+ "/DCIM/Camera/1.png";
File file = new File(path);
if (file != null && file.exists()) {
mulentity.addPart("file", new FileBody(file));
}
((HttpPost) myreq).setEntity(mulentity);
}
}
try {
response = myClient.execute(myreq);
}
int res = response.getStatusLine().getStatusCode();
if (res != HttpStatus.SC_OK) {
msg.what = WebSerState.Fail;
} else {
msg.what = WebSerState.Succeed;
HttpEntity entity = response.getEntity();
if (entity != null) {
try {
else {
//这里是返回的数据,返回的是这张图片的2进制流,这里写对吗
InputStream in = entity.getContent();
byte[] mcon=CameraDlg.readStream(in);
bundle.putByteArray(ResTag,mcon);
}
}
}

};
}

上面原本代码很多,我把一些东西删了 比如那些上面catch之类的,方便观察.....

在activity的会执行这个函数

public void my(Bundle bundle)
{
byte[] a=bundle.getByteArray(HttpHelp.ResTag); //这个有值的,但是貌似那数组跟原来的都不一样了,我观察要是图片的 人家是什么 1~10000 然后又10000-20000 之类的,但是我调试我发现这个直接就是一排数组,长度才13....
int ad=a.length;
Log.i("t460470591",ad+"");
Bitmap aw= BitmapFactory.decodeByteArray(a, 0, a.length); //我观察了好几次 这个值是null
ImageView aA= (ImageView) findViewById(R.id.s);
aA.setImageBitmap(aw);

}


下面是C# ashx的接收方法

if (context.Request.Files.Count > 0)
{
context.Response.ContentType = "image/jpeg";
HttpFileCollection files =context.Request.Files;
HttpPostedFile img=files.Get(0);
byte[] bytes = new byte[img.ContentLength];
using (BinaryReader reader = new BinaryReader(img.InputStream, Encoding.UTF8))
{
bytes = reader.ReadBytes(0);
}
//我这里要把接收的图片 转为2进制,存到数据库


context.Response.OutputStream.Write(bytes,0,img.ContentLength); //这句话的意思是,我吧图片转成了2进制然后返回到安卓,安卓接收了,看看能不能把二进制在转到图片,结果就失败了,就出现了上面那问题
return;

}

...全文
185 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
baidu_18750827 2014-08-01
  • 打赏
  • 举报
回复
后来 我添加了一句,File file = new File(path,"utf-8"); 还是不行
baidu_18750827 2014-08-01
  • 打赏
  • 举报
回复
这个是我跟踪到的数据


人家正常的是


那个什么编码,我记得我指定了utf-8的 后来我看看,貌似没发现这个怎么指定编码的
baidu_18750827 2014-08-01
  • 打赏
  • 举报
回复
两边都是UTF-8啊
licai1210 2014-08-01
  • 打赏
  • 举报
回复
看看是不是客户端编码的问题

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧