朋友们,我试图将文件从Android设备上传到server.i尝试使用模拟器和localhost,但它无法正常工作。 我在本地系统中使用wamp服务器。我上传文件的android代码如下。我需要将文件从模拟器上传到localsystem
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!";
}
?>
我在wamp文件夹的www目录中有一个名为uploads的文件夹。我需要将上传的文件存储在此文件夹中。
但是当我运行android程序时,它显示错误消息为
"java.net.Connect Exception: localhost/127.0.0.1:80 - Connection refused"
任何人都可以给我一个解决方案,让我知道我的编码足以实现我的目标。提前谢谢
答案 0 :(得分:1)
user664525
从"java.net.Connect Exception: localhost/127.0.0.1:80 - Connection refused"
开始,Android似乎试图引用自己的本地主机而不是服务器的地址。
您需要提供服务器机器的IP地址(在您的情况下是您自己的系统,例如192.168.40.24),而不是代码中的 localhost 。
答案 1 :(得分:0)
在模拟器中,SDCard没有读/写/执行权限,遗憾的是无法为模拟器的SD卡设置这些权限。要测试您的代码,我建议您将文件放在应用程序的asset
中,然后使用AssetManager
获取该文件的参考并上传。
通过这种方式,您可以至少测试上传算法,我相信如果能够工作,它也适用于实际设备。
答案 2 :(得分:0)
更改以下代码行:
String urlString="http://localhost/uploads/upload.php";
到
String urlString="http://10.0.2.2:portno/uploads/upload.php";
说明:
请注意,IP地址10.0.2.2是指Localmachine的IP地址。 而localhost引用模拟器的IP本身。 此外,“portno”通常是您的服务器服务通常运行的帖子80(默认情况下)..
HOPE这有助于!! !! ;)