上传音频文件时使用 HttpURLConnection 传递参数
Passing parameters with HttpURLConnection when Uploading audio file
当我从一个堆栈溢出答案中获取代码时。
Send .txt file, document file to the server in android
修改以下代码,我正在尝试POST audio/video 文件到在线php 服务器。一切正常文件正在正确上传并保存在数据库中但是问题是我也在发送文件参数以便我可以识别哪个用户正在发送录音, (user_id and candidate_id and file_type) 但在数据库中它们的值为 null/0。
请有人告诉我我的代码有什么问题为什么它不能用于参数。当我用 POSTMAN 检查我的 php 端服务器时,所有值都正确保存在文件中。
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传递参数有明确答案
当我从一个堆栈溢出答案中获取代码时。 Send .txt file, document file to the server in android
修改以下代码,我正在尝试POST audio/video 文件到在线php 服务器。一切正常文件正在正确上传并保存在数据库中但是问题是我也在发送文件参数以便我可以识别哪个用户正在发送录音, (user_id and candidate_id and file_type) 但在数据库中它们的值为 null/0。
请有人告诉我我的代码有什么问题为什么它不能用于参数。当我用 POSTMAN 检查我的 php 端服务器时,所有值都正确保存在文件中。
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传递参数有明确答案