gpt4 book ai didi

android - 如何在 android 中拆分 InputStream/OutPutStream

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

我正在向服务器上传流。但是我的Input-stream 包含一个大视频文件。所以我想将它拆分为不同的 Input-stream,然后我将它们一个接一个地发送。

我已经在 J​​ava 中解决了 TeeOutputStream(我不知道它在 java 中是如何工作的)的问题。但它在 android 中不存在。像往常一样非常感谢任何帮助

已更新

请不要建议我手动方式。

最佳答案

您不必拆分输入或输出流。您可以上传带有多部分实体的大文件。在多部分实体中有一个类 FileEntity 负责上传文件

我有一个多部分实体的代码,请参见下面的代码。

public class uploadFile extends AsyncTask<Void, Void, Boolean> {
private final ProgressDialog dialog = new ProgressDialog(parentActivity);

protected void onPreExecute() {
this.dialog.setMessage("Uploading file");
this.dialog.setCancelable(false);
this.dialog.show();
}

@Override
protected Boolean doInBackground(Void... arg0) {

try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(URLS.PRESCRIPTION_POST_URL);
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);

reqEntity.addPart("title", new StringBody("This is a title of video file"));
try {
File f = new File(Environment.getExternalStorageDirectory(), "your file name with extension");

FileBody body = new FileBody(f);
reqEntity.addPart("parameter that server will read", body);

} catch (Exception e) {
reqEntity.addPart("parameter that server will read", new StringBody(""));
}

reqEntity.addPart("description", new StringBody("description"));

postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);

BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String sResponse; StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
Log.v("Response for POst", s.toString());
return true;
} catch (Exception e) {
Log.e("MyPharmacyOptions", "Error :: " + e);
}
return false;
}

@Override
protected void onPostExecute(Boolean result) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
if (result) {
Toast.makeText(parentActivity,
"File uploaded successfully", Toast.LENGTH_LONG)
.show();

} else {
Toast.makeText(parentActivity, "Your Request not complete",
Toast.LENGTH_LONG).show();
}
}
}

要使用 MultipartEntity,您需要一个 jar 文件 httpmime-4.1.2.jar

还有另一种选择

HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;

String pathToOurFile = "/sdcard/file_to_send.mp3"; //complete path of file from your android device
String urlServer = "URL of your server";// complete path of server
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";

try
{
FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );

URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();

// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);

// Enable POST method
connection.setRequestMethod("POST");

connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

outputStream = new DataOutputStream( connection.getOutputStream() );
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
outputStream.writeBytes(lineEnd);

bytesAvailable = fileInputStream.available();

byte []buffer = new byte[4096];
int read = 0;
while ( (read = fileInputStream.read(buffer)) != -1 ) {
outputStream.write(buffer, 0, read);
}

outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

// Responses from the server (code and message)
serverResponseCode = connection.getResponseCode();
serverResponseMessage = connection.getResponseMessage();

fileInputStream.close();
outputStream.flush();
outputStream.close();
}
catch (Exception ex)
{
//Exception handling
}

关于android - 如何在 android 中拆分 InputStream/OutPutStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9886556/

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