Java 文件未从android上载到php服务器

Java 文件未从android上载到php服务器,java,php,android,Java,Php,Android,嗨,我写了一个代码,如下所示,用于将文件从我的android手机上传到php服务器。应用程序正在运行,没有任何错误,但文件未上载到服务器。我哪里出错了 公共类上载扩展了活动{ HttpURLConnection connection = null; DataOutputStream outputStream = null; DataInputStream inputStream = null; String pathToOurFile = "sdcard/Android/data/file.p

嗨,我写了一个代码,如下所示,用于将文件从我的android手机上传到php服务器。应用程序正在运行,没有任何错误,但文件未上载到服务器。我哪里出错了

公共类上载扩展了活动{

HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;

String pathToOurFile = "sdcard/Android/data/file.pdf";
String urlServer = "http://mpss.csce.uark.edu/~smandava/upload.php";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary =  "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    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
    }


}
}

//upload.php

$target_path = "http://mpss.csce.uark.edu/~smandava/files/";
$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!";
}

$target_路径应该是文件系统路径,而不是web url

$target_path = '/home/smandava/public_html/files/'; // something like this

它运行时没有错误,因为您捕获了Java代码中的所有潜在错误。在代码的
catch
块中放入
Log
语句,查看失败的原因。您可以将代码发布到PHP文件中吗?另外,您尝试上载的文件大小是多少,以及php.ini中upload_max_filesize的值是多少?PHP脚本是否正常工作?抱歉,PHP代码不明显,因此您可以忽略请求code@Haphazard我写了日志声明,上面写着06-22 15:50:05.119:ERROR/ERROR tag(6613):/sdcard/Android/data/file.pdfSo,您有一条错误消息。使用堆栈跟踪跟踪问题。。或者将整个堆栈跟踪粘贴到此处,并告诉我们失败的行号。或者,看看下面AndrewR的答案。