Java 上传音频文件时通过HttpURLConnection传递参数
当我从一个堆栈溢出答案中获取代码时。 修改下面的代码,我试图将音频/视频文件发布到在线php服务器。一切正常文件正在正确上传并保存在数据库中,但问题是我也在发送文件参数,以便识别哪个用户正在发送录制,(用户id和候选人id以及文件类型),但在数据库中,它们的值为空/0 请有人告诉我我的代码有什么问题为什么它不适用于params。当我用POSTMAN检查我的php端服务器时,所有的值都正确地保存在文件中Java 上传音频文件时通过HttpURLConnection传递参数,java,php,android,parameter-passing,httpurlconnection,Java,Php,Android,Parameter Passing,Httpurlconnection,当我从一个堆栈溢出答案中获取代码时。 修改下面的代码,我试图将音频/视频文件发布到在线php服务器。一切正常文件正在正确上传并保存在数据库中,但问题是我也在发送文件参数,以便识别哪个用户正在发送录制,(用户id和候选人id以及文件类型),但在数据库中,它们的值为空/0 请有人告诉我我的代码有什么问题为什么它不适用于params。当我用POSTMAN检查我的php端服务器时,所有的值都正确地保存在文件中 try { FileInputStream fileInputStr
try {
FileInputStream fileInputStream = new FileInputStream(
sourceFile);
URL url = new URL(Config.URL_UPLOAD);
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("uploadedfile", fileName);
dos = new DataOutputStream(conn.getOutputStream());
//here am adding the params
param.put("user_id", Constants.user_id);
param.put("candidate_id", "candiidateid1");
param.put("file_type", Constants.file_type);
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(dos, "UTF-8"));
writer.write(getPostDataString(param));
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";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);
dialog.dismiss();
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
Log.i("uploadFile", "HTTP Response is : "
+ serverResponseMessage + ": " + serverResponseCode);
DataInputStream inStream = null;
if (serverResponseCode == 200) {
try {
Log.e("isStream = ", "" + inStream);
inStream = new DataInputStream(conn.getInputStream());
String str;
Log.e("isStream = ", "" + inStream);
while ((str = inStream.readLine()) != null) {
Log.e("Debug", "Server Response " + str);
}
inStream.close();
Log.e("isStream = ", "" + inStream);
} catch (IOException ioex) {
Log.e("Debug", "error: " + ioex.getMessage(), ioex);
}
}
// close the streams //
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
dialog.dismiss();
ex.printStackTrace();
Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
} catch (Exception e) {
dialog.dismiss();
e.printStackTrace();
Log.e("Upload file to server ",
"Exception : " + e.getMessage(), e);
}
请参阅下面的URL,以获得清晰的答案来传递参数
请参考下面的URL,以获得清晰的答案来传递参数
DataOutputStream是一个Java实用程序类,用于编码简单的Java值。PHP可能无法以本机方式处理这种编码,因此您应该寻找其他跨语言的方式来传输元数据。PHP可能无法以本机方式处理这种编码,所以您应该寻找其他跨语言的方式来传输元数据。