[英]File upload to .net c# server throws IOException
*大家好,我已經嘗試使用c#將文件上傳到.net服務器。 我只是使用c:/ inetpub / wwwroot作為服務器位置。
我的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代碼,結果相同。 問題出在哪兒?
這是一個普遍的問題。 實際上,您應該將asp.net Web應用程序托管在iis Web服務器中(在您的計算機上)並獲取計算機IP地址(通過cmd中的ipconfig命令),然后將您的IP地址而不是"http://127.0.0.1/AndroidUpload/..."
。 就像"http://192.168.1.2/AndroidUpload/..."
Android模擬器無法理解127.0.0.1(本地地址)
編輯:
我不怎么用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")
}
}
要將仿真器連接到PC Web服務器,您需要遵循Android當前使用的尋址約定 。 更具體地說, 10.0.2.2
是您要使用的地址。
還要確保您的AndroidManifest.xml
包含Internet使用權限。 干杯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.