PHP Android上载失败
我目前正在使用hosting24.com实现php文件上传,并打算上传mp4文件。用于测试的html文件,可以正常工作。当Android设备执行时,它会失败,并显示$name=$\u文件[“upload\u file”]为空。你能告诉我有没有其他因素阻止我们成功上传 下面是我的代码PHP Android上载失败,php,android,upload,Php,Android,Upload,我目前正在使用hosting24.com实现php文件上传,并打算上传mp4文件。用于测试的html文件,可以正常工作。当Android设备执行时,它会失败,并显示$name=$\u文件[“upload\u file”]为空。你能告诉我有没有其他因素阻止我们成功上传 下面是我的代码 public String uploadFIle(File file){ String result = ""; HttpURLConnection conn = null;
public String uploadFIle(File file){
String result = "";
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
String fileName = file.getName();
int bytesRead, bytesAvailable, bufferSize;
int serverResponseCode = 0;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(file);
URL url = new URL("http://www.gallicalab.com/upload.php");
// 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);
}
// 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();
InputStream is = conn.getInputStream();
if(serverResponseCode==200){
BufferedReader reader = new BufferedReader(new InputStreamReader( is, "utf-8"), 8192);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
result = sb.toString();
}
// result = serverResponseMessage;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
服务器端
<?php
error_reporting(E_ALL);
$target_path = "target/";
// $target_path = $target_path . basename( $_FILES['file']['name']);
$name = $_FILES["uploaded_file"]["name"];
// echo $name."<br>";
$ext = end(explode(".", $name));
// echo $ext."<br>";
$randname = random_string(30);
// echo $randname."<br>";
$fname = $randname . "." .$ext;
//echo $fname."<br>";
$target_path = $target_path .$fname;
// echo $target_path."<br>";
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path)) {
//echo "The file ". basename( $_FILES['file']['name'])." has been uploaded";
//echo random_string(50);
// $arr = array ('data'=>'http://gallicalab.com/target/'.$_FILES['file']['name']);
$arr = array ('data'=>'http://gallicalab.com/target/'.$fname);
echo json_encode($arr); // {"a":1,"b":2,"c":3,"d":4,"e":5}
}
else {
echo "There was an error uploading the file, please try again!";
}
function random_string($length) {
$key = '';
$keys = array_merge(range(0, 9), range('a', 'z'));
for ($i = 0; $i < $length; $i++) {
$key .= $keys[array_rand($keys)];
}
return $key;
}
?>
能否将服务器端实际接收到的内容记录到文件中?Android应用程序可能未正确发出请求 试试这个:
<?php
error_reporting(E_ALL);
$h = fopen('upload.log', 'w');
fwrite($h, print_r($_POST, true) ."\r\n---\r\n");
fwrite($h, print_r($_FILES, true));
fclose($h);
// rest of your code
在服务器端:
$objFile = & $_FILES["file"];
$strPath = basename( $objFile["name"] );
if( move_uploaded_file( $objFile["tmp_name"], $strPath ) ) {
print "The file " . $strPath . " has been uploaded.";
} else {
print "There was an error uploading the file, please try again!";
}
来源:Array()--Array()即使使用你的方法,它仍然是空的,我正在使用hosting24.com。我也发布了这个问题。。。。也许是这样的:
$objFile = & $_FILES["file"];
$strPath = basename( $objFile["name"] );
if( move_uploaded_file( $objFile["tmp_name"], $strPath ) ) {
print "The file " . $strPath . " has been uploaded.";
} else {
print "There was an error uploading the file, please try again!";
}