gpt4 book ai didi

php - 从android上传大视频文件到服务器

转载 作者:行者123 更新时间:2023-11-29 15:50:40 25 4
gpt4 key购买 nike

我知道如何从 android 上传文件,我可以使用以下代码来完成

private void doFileUpload(MessageModel model) {
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;// 1 MB
String responseFromServer = "";

String imageName = null;
try {
// ------------------ CLIENT REQUEST
File file = new File(model.getMessage());
FileInputStream fileInputStream = new FileInputStream(file);
AppLog.Log(TAG, "File Name :: " + file.getName());
String[] temp = file.getName().split("\\.");
AppLog.Log(TAG, "temp array ::" + temp);
String extension = temp[temp.length - 1];
imageName = model.getUserID() + "_" + System.currentTimeMillis()
+ "." + extension;

// open a URL connection to the Servlet
URL url = new URL(Urls.UPLOAD_VIDEO);
// 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=\"uploaded_file\";filename=\""
+ imageName + "\"" + lineEnd);
Log.i(TAG, "Uploading starts");
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) {
// Log.i(TAG, "Uploading");
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
AppLog.Log(TAG, "Uploading Vedio :: " + imageName);
}
// 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");
Log.i(TAG, "Uploading ends");
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
ex.printStackTrace();
Log.e("Debug", "error: " + ex.getMessage(), ex);
} catch (IOException ioe) {
ioe.printStackTrace();
Log.e("Debug", "error: " + ioe.getMessage(), ioe);
}
// ------------------ read the SERVER RESPONSE ----------------
try {
inStream = new DataInputStream(conn.getInputStream());
String str;

while ((str = inStream.readLine()) != null) {
Log.e("Debug", "Server Response " + str);
try {
final JSONObject jsonObject = new JSONObject(str);
if (jsonObject.getBoolean("success")) {
handler.post(new Runnable() {
public void run() {
try {
Toast.makeText(getApplicationContext(),
jsonObject.getString("message"),
Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}

}
});
} else {
handler.post(new Runnable() {
@Override
public void run() {
try {
Toast.makeText(getApplicationContext(),
jsonObject.getString("message"),
Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}

}
model.setMessage(imageName);
onUploadComplete(model);
inStream.close();

} catch (IOException ioex) {
ioex.printStackTrace();
Log.e("Debug", "error: " + ioex.getMessage(), ioex);
}
manageQueue();
}

该代码非常适合短视频,但无法上传大文件,我也不知道为什么。:(

我知道只问为什么我的代码不起作用是一种不好的做法,但在这里我要问的是为什么代码对于大文件的行为不同。

我还在 StackOverFlow 上检查了其他答案,但没有发现我的代码有任何缺陷。

谢谢

最佳答案

你可以试试HttpClient jar下载最新的 HttpClient jar,将其添加到您的项目中,然后使用以下方法上传视频:

    private void uploadVideo(String videoPath) throws ParseException, IOException {

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(YOUR_URL);

FileBody filebodyVideo = new FileBody(new File(videoPath));
StringBody title = new StringBody("Filename: " + videoPath);
StringBody description = new StringBody("This is a video of the agent");
StringBody code = new StringBody(realtorCodeStr);

MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("videoFile", filebodyVideo);
reqEntity.addPart("title", title);
reqEntity.addPart("description", description);
reqEntity.addPart("code", code);
httppost.setEntity(reqEntity);

// DEBUG
System.out.println( "executing request " + httppost.getRequestLine( ) );
HttpResponse response = httpclient.execute( httppost );
HttpEntity resEntity = response.getEntity( );

// DEBUG
System.out.println( response.getStatusLine( ) );
if (resEntity != null) {
System.out.println( EntityUtils.toString( resEntity ) );
} // end if

if (resEntity != null) {
resEntity.consumeContent( );
} // end if

httpclient.getConnectionManager( ).shutdown( );
} // end of uploadVideo( )

关于php - 从android上传大视频文件到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30209749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com