【发布时间】:2023-03-23 08:44:01
【问题描述】:
我已经苦苦挣扎了大约一周,尝试用不同的方式上传我的音频文件和用户数据,以便确定要在数据库中保存什么。
文件已成功上传,但我无法获取用户名。
@user5155835 here 有同样的问题,显然已经被他解决了,但我无法让它工作。
请我需要一些帮助。我已经废弃了这段代码并使用了不同的其他方法,但我永远看不到 POST 变量。
在我的挣扎中,我再次回到这段代码,更不用说我正在处理多个 asyncTask 进程和编译gradle 中的库是一场噩梦。
在安卓中
FileInputStream fileInputStream = new FileInputStream(new File(existingFileName));
// open a URL connection to the Servlet
URL url = new URL(urlString);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
// Allow Inputs
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(false);
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + existingFileName + "\"" + 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);
dos.writeBytes("Content-Disposition: form-data; name=\"user\"" +params[0] +"\""+lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
// close streams
Log.e("Debug", "File is written");
fileInputStream.close();
dos.flush();
dos.close();
在服务器中。
$email = $_POST['user'];
echo "email is ".$email;
$strSQL = "SELECT * FROM member WHERE email = '".$email."' ";
$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
if($objResult)
{
//upload file
}
来自服务器
E/Debug: File is written
E/Debug: Server Response email is
【问题讨论】:
-
很奇怪你有问题。用于发布文件和一些参数的代码经常在 stackoverflow 上发布。那为什么找不到呢?原则上,每个参数及其值都必须在两条边界线之间发送。就像文件一样。所以谷歌。
标签: php android post multipart