如何在Android的分裂的InputStream / OutputStream的 [英] how to split an InputStream/OutPutStream in android

查看:274
本文介绍了如何在Android的分裂的InputStream / OutputStream的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我上载流server.But我的输入流包含一个大的视频file.So我想它在不同的输入流拆分,然后我会一个给他们的。

I am uploading a stream to server.But my Input-stream contain a big video file.So i want to split it in different Input-stream and then i will send them one by one.

我已经通过Java中的 TeeOutputStream 的问题(我不如何在Java工作)去这样做this.But它不存在的Andr​​oid。
任何帮助非常AP preciated照常

I have gone through a question that TeeOutputStream(i do not how it work in java) in Java for doing this.But it does not exist in android. Any help much appreciated as usual

更新

请DONOT建议我手工方式。

Please donot suggest me manual way.

推荐答案

您不必拆分输入或输出流。
您可以上传与多部分entity.In多部分实体较大的文件中还有一类FileEntity它负责将文件上传

You do not have to split up the input or output stream. You can upload a large file with multipart entity.In Multipart entity there is a class FileEntity which is responsible to upload a file

我有一个code多部分实体见下文code。

I have a code for multipart entity see below code.

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

To use MultipartEntity you will required a jar file 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的分裂的InputStream / OutputStream的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆