【发布时间】:2016-12-22 21:27:59
【问题描述】:
当我从堆栈溢出答案之一中获取代码时。 Send .txt file, document file to the server in android
修改以下代码,我正在尝试将音频/视频文件发布到在线 php 服务器。一切正常,文件正在正确上传并保存在数据库中,但问题是我也在发送文件参数,以便我可以识别哪个用户正在发送录音,(user_id 和 Candidate_id 和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);
}
【问题讨论】:
-
DataOutputStream 是一个 Java 实用程序类,用于对简单的 Java 值进行编码。 PHP 可能无法原生处理这种编码,因此您应该寻找其他跨语言的方式来传输元数据。
标签: java php android parameter-passing httpurlconnection