android - 音频文件上传到android中的实时服务器
问题描述
当我尝试运行以下代码时,它没有显示任何错误。它的日志“文件Witten”。但文件未上传到服务器。虽然我在本地尝试相同的代码,但 PHP 代码能够上传。请帮助。在此先感谢
包 com.example.file_upload_demo;导入android.os.Handler;导入android.os.Message;enter code here
导入android.util.Log;导入 java.io.DataInputStream;导入 java.io.DataOutputStream;导入java.io.File;导入 java.io.FileInputStream;导入 java.io.IOException;导入 java.net.HttpURLConnection;导入 java.net.MalformedURLException;导入 java.net.URL;公共类 FileUploadUtility { 静态字符串 SERVER_PATH = "example.com";
enter code here
public static void doFileUpload(final String selectedPath, final Handler handler) {
//
new Thread(new Runnable() {
@Override
public void run() {`enter code here`
HttpsTrustManager.allowAllSSL();
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String lineEnd = "rn";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
String responseFromServer = "";
try {
//------------------ CLIENT REQUEST
FileInputStream fileInputStream = new FileInputStream(new File(selectedPath));
// open a URL connection to the Servlet
URL url = new URL(SERVER_PATH);
// 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", "application/json;charset=" + boundary);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"mp3File\";filename=\""
+ selectedPath + "\"" + 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);
// close streams
Log.e("Debug", "File is written");
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
Log.e("Debug", "error: " + ex.getMessage(), ex);
sendMessageBack(responseFromServer, 0, handler);
return;
} catch (IOException ioe) {
Log.e("Debug", "error: " + ioe.getMessage(), ioe);
sendMessageBack(responseFromServer, 0, handler);
return;
}
responseFromServer = processResponse(conn, responseFromServer);
sendMessageBack(responseFromServer, 1, handler);
}
}).start();
}
private static String processResponse(HttpURLConnection conn, String responseFromServer) {
DataInputStream inStream;
try {
inStream = new DataInputStream(conn.getInputStream());
String str;
while ((str = inStream.readLine()) != null) {
responseFromServer = str;
}
inStream.close();
} catch (IOException ioex) {
Log.e("Debug", "error: " + ioex.getMessage(), ioex);
}
return responseFromServer;
}
static void sendMessageBack(String responseFromServer, int success, Handler handler) {
Message message = new Message();
message.obj = responseFromServer;
message.arg1 = success;
handler.sendMessage(message);
}`enter code here`
}
解决方案
试试这个:1)在你的应用程序中打开清单文件夹
2)添加
<uses-permission android:name="android.permission.INTERNET" />
3) 添加<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
推荐阅读
- java - 有没有办法随机化 for 循环中的循环增加?
- php - 在嵌套的 IF 语句中 PHP 重定向不起作用
- java - 使用 JPA 在 Spring Boot 中生成的序列 ID(来自 DB 序列)由其他服务器/实例共享并导致违反唯一约束
- python - 使用 conda 安装 face_recognition 时遇到问题
- amazon-web-services - 在新的 EC2 实例上自动附加和挂载 EBS 卷
- excel - 基于switch的excel图表
- python - 在 Python 中重构字典的层次结构?
- sql - 将行的值填充到按日期分区的新列中
- python - 如何在 Python 中正确地进行单元测试
- python - 将结果响应从另一个应用程序代码保存到您的应用程序