使用emulator和localsystem在android中上载文件

使用emulator和localsystem在android中上载文件,android,Android,嗨,朋友们,我正在尝试将一个文件从android设备上传到服务器。我用emulator和localhost进行了尝试,但没有成功。 我在本地系统中使用wamp服务器。下面给出了我上传文件的android代码。我需要上传文件从模拟器到本地系统 upload.java HttpURLConnection conn = null; DataOutputStream dos = null; DataInputStream inStream = null; String existingFileName

嗨,朋友们,我正在尝试将一个文件从android设备上传到服务器。我用emulator和localhost进行了尝试,但没有成功。 我在本地系统中使用wamp服务器。下面给出了我上传文件的android代码。我需要上传文件从模拟器到本地系统

upload.java

HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String existingFileName =FileName.getText().toString();
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary =  "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
//String responseFromServer = "";
String urlString="http://localhost/uploads/upload.php";

try
{

FileInputStream fileInputStream = new FileInputStream(new File(existingFileName) );
 // open a URL connection to the Servlet
 URL url = new URL(urlString);

 conn = (HttpURLConnection) url.openConnection();

 conn.setDoInput(true);

 conn.setDoOutput(true);

 conn.setUseCaches(false);

 conn.setRequestMethod("POST");
 conn.setRequestProperty("Connection", "Keep-Alive");
 conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
 dos = new DataOutputStream( conn.getOutputStream() );
 dos.writeBytes(twoHyphens + boundary + lineEnd);
 dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + existingFileName + "\"" + lineEnd);
 dos.writeBytes(lineEnd);

 bytesAvailable = fileInputStream.available();
 bufferSize = Math.min(bytesAvailable, maxBufferSize);
 buffer = new byte[bufferSize];

 bytesRead = fileInputStream.read(buffer, 0, bufferSize);
 while (bytesRead > 0)
 {
  dos.write(buffer, 0, bufferSize);
  bytesAvailable = fileInputStream.available();
  bufferSize = Math.min(bytesAvailable, maxBufferSize);
  bytesRead = fileInputStream.read(buffer, 0, bufferSize);
 }

 dos.writeBytes(lineEnd);
 dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

 Log.e("Debug","File is written");
 fileInputStream.close();
 dos.flush();
 dos.close();
 Toast.makeText(Upload.this, "Files have been uploaded successfully",Toast.LENGTH_SHORT).show();
}
catch (MalformedURLException ex)
{
     Log.e("Debug", "error: " + ex.getMessage(), ex);
}
catch (IOException ioe)
{
     Log.e("Debug", "error: " + ioe.getMessage(), ioe);
}

try {
      inStream = new DataInputStream ( conn.getInputStream() );
      String str;

      while (( str = inStream.readLine()) != null)
      {
           Log.e("Debug","Server Response "+str);
      }
      inStream.close();

}
catch (IOException ioex)
{
     Log.e("Debug", "error: " + ioex.getMessage(), ioex);
}
这是我的php代码

upload.php

<?php

 $target_path = "uploads/";

 $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

 if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
    " has been uploaded";
 } 

 else{
      echo "There was an error uploading the file, please try again!";
   }
   ?>

有谁能给我一个解决方案,让我知道我的编码足以实现我的目标。提前感谢emulator中的

,SD卡没有读/写/执行权限,不幸的是,无法为emulator的SD卡设置这些权限。要测试您的代码,我建议将您的文件放在应用程序的
asset
中,并使用
AssetManager
获取该文件的引用并上载它


通过这种方式,您至少可以测试上传算法,并且我确信如果这种方法有效,它也会在实际设备上工作。

在emulator中,SD卡没有读/写/执行权限,不幸的是,无法为emulator的SD卡设置这些权限。要测试您的代码,我建议将您的文件放在应用程序的
asset
中,并使用
AssetManager
获取该文件的引用并上载它

通过这种方式,您至少可以测试上传算法,我确信如果它能工作,它也能在实际设备上工作。

user664525 从
“java.net.Connect异常:localhost/127.0.0.1:80-连接被拒绝”
中,Android似乎试图引用自己的localhost,而不是您的服务器地址

您需要在代码中提供服务器机器的IP地址(在您的情况下,您自己的系统是192.168.40.24),而不是本地主机。

用户664525 从
“java.net.Connect异常:localhost/127.0.0.1:80-连接被拒绝”
中,Android似乎试图引用自己的localhost,而不是您的服务器地址


您需要在代码中提供服务器机器的IP地址(在您的情况下,您自己的系统是192.168.40.24),而不是本地主机。

更改以下代码行:

String urlString="http://localhost/uploads/upload.php";

说明:

注意这里的IP地址10.0.2.2指的是本地机器的IP地址。 而localhost指的是仿真器的IP本身。 此外,“端口号”通常是服务器服务运行的Post编号,通常为80(默认情况下)


希望这对……有帮助!!;)

更改以下代码行:

String urlString="http://localhost/uploads/upload.php";

说明:

注意这里的IP地址10.0.2.2指的是本地机器的IP地址。 而localhost指的是仿真器的IP本身。 此外,“端口号”通常是服务器服务运行的Post编号,通常为80(默认情况下)


希望这对……有帮助!!;)

但是我得到的错误是“java.net.Connect异常:localhost/127.0.0.1:80-拒绝连接”我如何解决ituse 127.0.0.1而不是localhost但是我得到的错误是“java.net.Connect异常:localhost/127.0.0.1:80-拒绝连接”如何解决iTunes 127.0.0.1而不是LocalHost的问题文件上载是否将成为应用程序的一部分,或者您只是为了测试目的尝试在模拟器/本地PC之间交换文件?文件上载是否将成为应用程序的一部分,或者您只是为了测试目的尝试在模拟器/本地PC之间交换文件?我是否必须提及wampserver使用ip地址运行的任何端口号,如果是,我该怎么办it@user664525ServerIP:PortNo。比如说192.168.40.24:443我想你不明白我的问题,我的系统是独立的,没有连接到任何网络。我在其中运行模拟器和本地php服务器。现在我如何在android中指定这个本地服务器地址page@user664525您只需使用
ifconfig
(在linux中)查找系统的IP地址或者
ipconfig
(在windows中)&使用该IP地址代替
localhost
我刚刚从你最近的一堆帖子中删除了签名链接,所以我想指出,这些链接在堆栈溢出时是被禁止的。从“请不要在帖子中使用签名或标语。您的每篇帖子都已使用标准用户卡“签名”,该卡直接链接到您的用户页面。”谢谢。我是否必须提及wampserver使用ip地址运行的任何端口号,如果是,我该怎么办it@user664525ServerIP:PortNo。比如说192.168.40.24:443我想你不明白我的问题,我的系统是独立的,没有连接到任何网络。我在其中运行模拟器和本地php服务器。现在我如何在android中指定这个本地服务器地址page@user664525您只需使用
ifconfig
(在linux中)查找系统的IP地址或者
ipconfig
(在windows中)&使用该IP地址代替
localhost
我刚刚从你最近的一堆帖子中删除了签名链接,所以我想指出,这些链接在堆栈溢出时是被禁止的。从“请不要在帖子中使用签名或标语。您的每一篇帖子都已经用标准用户卡“签名”,该卡直接链接到您的用户页面。”谢谢。
String urlString="http://10.0.2.2:portno/uploads/upload.php";