2013-07-12 53 views
1

*大家好, 我试过使用c#将文件上传到.net服务器。我只是使用c:/ inetpub/wwwroot作为服务器位置。文件上传到.net c#服务器抛出IOException

我的Java代码,

public class MainActivity extends Activity { 
    InputStream inputStream; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.two); 
     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 

     bitmap.compress(Bitmap.CompressFormat.PNG,90, stream); 
     byte[] byte_arr = stream.toByteArray(); 
     String image_str = Base64.encodeBytes(byte_arr); 
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     nameValuePairs.add(new BasicNameValuePair("image",image_str)); 

     try { 
      HttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost("http://127.0.0.1/AndroidUpload/uploadImage.cs"); 
      httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      HttpResponse response = httpClient.execute(httpPost); 
      String the_response_string = convertResponseToString(response); 


      Toast.makeText(this, "Response"+the_response_string, Toast.LENGTH_SHORT).show(); 
     } catch (UnsupportedEncodingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      Toast.makeText(this,"Error1", Toast.LENGTH_SHORT).show(); 
     } catch (ClientProtocolException e) { 
      //TODO Auto-generated catch block 
      e.printStackTrace(); 
      Toast.makeText(this,"Error2", Toast.LENGTH_SHORT).show();} 
     catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      Toast.makeText(this,"Error3", Toast.LENGTH_SHORT).show(); 
     } 

    } 
    public String convertResponseToString(HttpResponse response)throws IllegalStateException,IOException { 
     // TODO Auto-generated method stub 

     String res = ""; 
     StringBuffer buffer = new StringBuffer(); 
     inputStream = response.getEntity().getContent(); 
     int contentLength = (int) response.getEntity().getContentLength(); 
     Toast.makeText(this, "ContentLength"+contentLength, Toast.LENGTH_SHORT).show(); 

     if(contentLength<0){ 
        } 
     else{ 
      byte[] data = new byte[512]; 
      int len = 0; 

      try { 
       while(-1 != (len=inputStream.read(data))){ 
        buffer.append(new String(data,0,len)); 
       } 

       inputStream.close(); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      res = buffer.toString(); 

      Toast.makeText(this, "Result"+res, Toast.LENGTH_SHORT).show(); 
     } 

     return res; 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
} 

而且我对上传的C#代码,

protected void Page_Init(object sender, EventArgs e) 
{ 
    string vTitle = ""; 
    string vDesc = ""; 
    string FilePath = Server.MapPath("/files/two.png");   
    if (!string.IsNullOrEmpty(Request.Form["title"])) 
    { 
    vTitle = Request.Form["title"]; 
    } 
    if (!string.IsNullOrEmpty(Request.Form["description"])) 
    { 
    vDesc = Request.Form["description"]; 
    } 

    HttpFileCollection MyFileCollection = Request.Files; 
    if (MyFileCollection.Count > 0) 
    { 
    // Save the File 
    MyFileCollection[0].SaveAs(FilePath); 
    } 
} 

运行时,它抛出IOException异常。该文件不上传到该文件夹​​。同样的结果也尝试了PHP代码。 问题在哪里?

+0

请显示IOException(stacktrace)。 – home

+0

07-12 11:59:08.594:W/System.err(491):org.apache.http.conn.HttpHostConnectException:连接到http://127.0.0.1拒绝 W/System.err(491):在org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection (DefaultClientConnectionOperator.java:178) W/System.err的(491):\t在org.apache.http.impl.conn.AbstractPoolEntry.open( AbstractPoolEntry.java:164) W/System.err的(491):\t在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) W/System.err的(491):\t在java.net.Socket.connect(Socket.java:1055) – VijayaRagavan

+1

还有一些。他们都是警告,并没有显示错误。 – VijayaRagavan

回答

1

要从仿真器连接到PC的Web服务器,您需要按照目前使用的由Android的addressing convention。更具体地说,10.0.2.2是你想要使用的地址。

另外请确保您的AndroidManifest.xml包含Internet使用权限。欢呼声

+1

它使用IP 10.0.2.2没有错误的工作。但该文件不会来到受到影响的文件夹。 – VijayaRagavan

+1

你可能想保存到'string FilePath = Server.MapPath(“〜/ files/two.png”);' - 注意**〜**。 为了更好的解释,你可以看看这里:http://stackoverflow.com/questions/275781/server-mappath-server-mappath-server-mappath-server-mappath – Trogvar

+1

我需要表达的是,我不交易与iis什么的。我只有这些代码和c:/ inetpub/wwwroot中的文件夹。在这个阶段还有什么需要做的吗? – VijayaRagavan

1

这是一个常见问题。事实上,你应该在iis网络服务器(在你的计算机)上托管你的asp.net web应用程序并获得你的计算机ip地址(通过ipconfig命令在cmd中),然后把你的ip地址代替"http://127.0.0.1/AndroidUpload/..."。它可以像"http://192.168.1.2/AndroidUpload/..."

Android模拟器无法理解127.0.0.1(将localAddress)

编辑:

我不如何​​upoad用asp.net web表单文件,但如果我是你,我会写一个简单的asp.net MVC应用程序,并在控制器中,我宣布一个动作是这样的:

[HttpPost] 
     public ActionResult UploadItem() 
     { 
      var httpPostedFileBase = Request.Files[0]; 
      if (httpPostedFileBase != null && httpPostedFileBase.ContentLength != 0) 
      { 
       //save file in server and return a string 
     return Content("Ok"); 
      } 
      else 
      { 
       return Content("Failed") 
      } 
    } 
+0

试过我的电脑ip地址,但IO异常抛出朋友。 – VijayaRagavan

+0

试过10.0.2.2。它可以工作,但文件没有上传。 – VijayaRagavan

+0

刚刚更新了我的答案。看看它是否工作 –