2013-07-12 24 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

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