Java Android使用php将文件上传到服务器
你好,我正在尝试将一个文件从android手机的外部存储器上传到变量upLoadServerUri中的url,如下所示Java Android使用php将文件上传到服务器,java,php,android,Java,Php,Android,你好,我正在尝试将一个文件从android手机的外部存储器上传到变量upLoadServerUri中的url,如下所示 final String upLoadServerUri = "http://www.www.com/UploadToServer.php"; URL url = new URL(upLoadServerUri); // Open a HTTP connection to the URL conn = (HttpURLConnection) url.openConnect
final String upLoadServerUri = "http://www.www.com/UploadToServer.php";
URL url = new URL(upLoadServerUri);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("uploaded_file", fileName);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
+ fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
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);
Log.d("Inside ByteRead", "Reading");
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
fileInputStream.close();
dos.flush();
dos.close();
下面的代码正在UploadToServer.php文件中使用,该文件是上面java代码中的目标url文件。我不了解php端,所以您能告诉我,当我们基于上面的java代码触发一个文件时,下面的代码是否能够存储数据。谢谢
<?php
// Where the file is going to be placed
$target_path = "recorded/";
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploaded_file']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
echo "filename: " . basename( $_FILES['uploaded_file']['name']);
echo "target_path: " .$target_path;
}
?>
请尝试改装。它简单易用。阅读本教程 请尝试改装。它简单易用。阅读本教程 我对php代码有意见。我觉得android代码很好。我对php代码有意见。我觉得android代码很好。php脚本正在向你的android应用程序回显信息。这样做是为了沟通。你没有在Android应用程序中阅读这些回音。因此,您不知道脚本中发生了什么。php脚本看起来不错。但是你的android代码缺少读取回声的部分。奇怪的是你没有告诉我们你是否可以上传文件。奇怪的是,你没有报告你的Android代码是如何运行的。代码和消息。@greenapps感谢您的回复。你能告诉我上面的php代码是否可以接受和存储传入的文件吗?你应该开始对我说的做出反应。php脚本正在向你的Android应用程序回显信息。这样做是为了沟通。你没有在Android应用程序中阅读这些回音。因此,您不知道脚本中发生了什么。php脚本看起来不错。但是你的android代码缺少读取回声的部分。奇怪的是你没有告诉我们你是否可以上传文件。奇怪的是,你没有报告你的Android代码是如何运行的。代码和消息。@greenapps感谢您的回复。你能告诉我上面的php代码是否可以接受和存储传入的文件吗?你应该开始对我说的做出反应。