从Android SD卡上传文件到网络服务器
问题描述:
everyone。我试图从手机上将图片文件上传到我的电脑上的Apache Web服务器,但没有成功。这里是我的代码:从Android SD卡上传文件到网络服务器
package com.testconnectivity;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
String pathToOurFile = "/sdcard/Confused.jpg";
String urlServer = "http://190.213.29.178/connectiontest1.php";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
try
{
FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile));
URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
fileInputStream.close();
outputStream.flush();
outputStream.close();
}
catch (Exception ex)
{
//Exception handling
Log.d("MainActivity", "Print " + ex.toString());
}
}
}
// connectiontest1.php
<?php
$target_path = "./";
$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!";
}
?>
可能有人请告诉我出了什么问题?我是否需要Web服务或者是否有其他问题?任何帮助将非常感激。
答
你有android.os.NetworkOnMainThreadException吗?
您应该尝试使用AsyncTask。这对我有效。
检查:How to fix android.os.NetworkOnMainThreadException?
+1
作为一般性建议,最好解释为什么这些东西连接起来,为什么使用AsyncTask可以解决这个问题。还有如何确认这是问题,原始海报应该在哪个方面看这个错误。 – EdC 2012-09-15 02:54:59
答
下面这样的代码:
//启用POST方法 connection.setRequestMethod( “POST”);
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form data;boundary="+boundary);
添加下列行:
connection.setRequestProperty( “UploadedFile的”,pathToOurFile);
对不起,试图上传我的php代码下的评论连接测试,但它没有工作。 – 2011-03-24 19:14:44
你会得到什么错误(在java端)? – MByD 2011-03-24 19:41:01
@MByD - 编译器或控制台没有注册错误。有关问题可能出在哪里的任何建议? – 2011-03-24 20:33:33