日期:2014-05-18 浏览次数:20794 次
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(); }
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; } }