Java 将变量从android类移动到php端时出错
我目前正在开发一个应用程序,在将变量从android传输到php脚本时遇到了一个问题。然而,连接已建立。任何帮助都将不胜感激 这是我的Java代码Java 将变量从android类移动到php端时出错,java,php,android,Java,Php,Android,我目前正在开发一个应用程序,在将变量从android传输到php脚本时遇到了一个问题。然而,连接已建立。任何帮助都将不胜感激 这是我的Java代码 // open a URL connection to the Servlet FileInputStream fileInputStream = new FileInputStream(sourceFile); URL url = new URL(upLoadServerUri); // Open a HTTP connection to t
// open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
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);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";name=\""
+ username + "\"" + 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);
这是我的php脚本
<?php
require("config.inc.php");
$file_path = "uploads/";
$file_path = $file_path . basename( $_FILES['uploaded_file']['filename']);
$user = $_FILES['username'];
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
$query = "INSERT INTO users ( username, password, picture ) VALUES ( '1234567', $user, $file_path ) ";
$stmt = $db->prepare($query);
$stmt->execute();
echo "success";
} else{
echo "fail";
}
?>
失败的原因是您没有正确准备查询:
$query = "INSERT INTO users ( username, password, picture ) VALUES ( ?, ?, ? ) ";
$stmt = $db->prepare($query);
$stmt->execute(array('1234567', $user, $file_path));
一点格式将大大有助于你的问题被看到,而不是隐藏在你的代码中。谢谢你的这句话,我编辑了它,减少了代码,这样人们可以很容易地通过它。事实上这不是问题,因为在修复了小语法问题后,它确实正确插入了。主要的问题是图像确实移动到了php脚本和我可以提取,而字符串参数我不能提取:/@AbdelrahmanMaged你在说哪个字符串你应该回显,看看哪个是空的字符串$user但是图像工作正常,我可以得到它的路径@meda@AbdelrahmanMaged执行var\u转储($\u文件)
在第二个内容配置中,您还编写了两次name
属性。。你正在上传多个文件吗?