读书人

asp.net怎么获取android上传的图片【很

发布时间: 2012-07-08 17:43:44 作者: rapoo

asp.net如何获取android上传的图片【很愁,一直搞不定】
用asp.net做的webservice,客户端是android, android端要上传头像(目前android端是通过http流的方式传输过来的),
并且保存到服务器上面,我不知道服务器需要如何去处理,能贴出代码吗。

有人说跟webservice没有关系,直接用ashx文件处理,但是我想知道,怎么处理呢,能贴出代码吗

这个问题,已经发了好几天,也好几便了,一直没人解决,我都已经没有分了,只有这么点了,请高手帮我解决吧。我愁呀

我把目前的代码都贴出来了,请大虾们帮我看一下呀。 服务器端还是怎么也拿不上传的图片

android端的代码是这样的:

Java code
HttpURLConnection con;URL url;String httpUrl="http://192.168.0.105/TUISONG/Service.asmx";InputStream in;byte[] buf=new byte[1024];try {in=new FileInputStream(new File(Environment.getExternalStorageDirectory()+"/treemenu.jpg"));url=new URL(httpUrl);con=(HttpURLConnection)url.openConnection();con.setConnectTimeout(20000);con.setReadTimeout(12000);  con.setRequestMethod("POST");  con.setDoOutput(true);  con.setDoInput(true);      OutputStream osw = con.getOutputStream();  while(in.read(buf)!=-1){  osw.write(buf);  }osw.flush();osw.close();in.close();  int code = con.getResponseCode();  System.out.println("code:"+code);    } catch (Exception e) {e.printStackTrace();}


服务器端代码:
C# code
public void ProcessRequest(HttpContext context)    {                context.Response.ContentType = "text/plain";        context.Response.Charset = "utf-8";        string uploadPath = HttpContext.Current.Server.MapPath(WebCommon.GetClientImagePath());        if (!System.IO.Directory.Exists(uploadPath))        {            System.IO.Directory.CreateDirectory(uploadPath);        }                System.IO.Stream stream = context.Request.InputStream;//这是你获得的流         byte[] buffer = new byte[stream.Length];        stream.Read(buffer, 0, buffer.Length);     //将流的内容读到缓冲区         System.IO.FileStream fs = new System.IO.FileStream(uploadPath + "test.jpg", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);        fs.Write(buffer, 0, buffer.Length);        fs.Flush();        fs.Close();      }     public bool IsReusable {        get {            return false;        }    }    


[解决办法]
首先确保存储的路径是对的,并且是在网站目录下,并且有写入权限。
另外
stream.Postion=0再进行读取试试
[解决办法]
这个之前也搞过,是用http方式写的,可以的。。

读书人网 >C#

热点推荐