无法使用多部分形式发送二进制数据(字符串参数+视频参数+图像参数)

时间:2014-04-04 17:59:12

标签: android http rest connection multipartform-data

我试图通过使用multipart-form发送一些二进制文件(1.44 MB视频和png图像)以及一些字符串参数时遇到了问题。问题是在写完标题和所有必要的东西后,在输出流上写字节时,它阻止我写别的东西。

你能告诉我我做错了什么!!

这是我的AsyncTask,它将数据发送到服务器

private class UploadUpAsyncTask extends AsyncTask<String, Void,
String>{

    private String path;

    private String lineend ="\r\n";        
    private String boundry = "****";       
    private String twoHiphens="--";        
    int bytesRead,bytesAvailable,bufferSize;       
    byte[] buffer;         
    int maxBufferSize = 1*1024*1024;
    public UploadUpAsyncTask(String path){            
        this.path =path;        
    }


    @Override
    protected String doInBackground(String... urls) {


        File file = new File(path);            
        File image = new File("/storage/emulated/0/DCIM/100MEDIA/error.png");          
        try {

            URL url = new URL(urls[0]);
            Log.d("UPLOAD", "URL ="+urls[0]);

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundry);
            conn.setRequestProperty("Authorization", "----------------------------");



            DataOutputStream out = new DataOutputStream(conn.getOutputStream());
            out.writeBytes(twoHiphens+boundry+lineend);
            out.writeBytes("Content-Disposition: form-data; name=\"user_id\""+lineend+lineend);
            out.writeBytes("1"+lineend);

            out.writeBytes(twoHiphens+boundry+lineend);
            out.writeBytes("Content-Disposition: form-data; name=\"preview_id\""+lineend+lineend);
            out.writeBytes("1"+lineend);

            out.writeBytes(twoHiphens+boundry+lineend);
            out.writeBytes("Content-Disposition: form-data; name=\"categories_id\""+lineend+lineend);
            out.writeBytes("2"+lineend);

            out.writeBytes(twoHiphens+boundry+lineend);
            out.writeBytes("Content-Disposition: form-data; name=\"title\""+lineend+lineend);
            out.writeBytes("Mama"+lineend);

            out.writeBytes(twoHiphens+boundry+lineend);
            out.writeBytes("Content-Disposition: form-data; name=\"tags\""+lineend+lineend);
            out.writeBytes("mama"+lineend);


            out.flush();


            out.writeBytes(twoHiphens+boundry+lineend);
            out.writeBytes("Content-Disposition: form-data; name=\"video\"; filename=\""+file.getName()+"\""+lineend);
            out.writeBytes(lineend);
            Log.d("UPLOAD", "Titlul video-ului ="+file.getName());



            //decoding of bytes from video
            FileInputStream file_stream = new FileInputStream(file);
            bytesAvailable =file_stream.available();
            bufferSize = Math.min(bytesAvailable,maxBufferSize);
            buffer = new byte[bufferSize];
            Log.d("UPLOAD", "Bytes Read Video =" +bytesRead);

            bytesRead = file_stream.read(buffer);
            //writting to outputstream
            while (bytesRead >0){
                out.write(buffer, 0, bytesRead);
                bytesRead=file_stream.read(buffer);

            }
            Log.d("UPLOAD", "Done Loading first buffer");

            file_stream.close();

            out.writeBytes(twoHiphens+boundry+lineend);
            out.writeBytes("Content-Disposition: form-data; name=\"thumb\"; filename=\""+image.getName()+"\""+lineend);
            out.writeBytes(lineend);
            Log.d("UPLOAD", "Titlul preview-ului ="+image.getName());

            //decodint image bytes
            FileInputStream image_stream = new FileInputStream(image);
            int bytesRead2;
            int bytesAvailable2, bufferSize2 ;
            bytesAvailable2 = image_stream.available();
            bufferSize2 = Math.min(bytesAvailable2, maxBufferSize);
            byte []buffer2 = new byte[bufferSize2];


            //writing to outputstream
            bytesRead2 = image_stream.read(buffer2);
            while(bytesRead2>0){
                out.write(buffer2, 0, bytesRead2); //                   bytesAvailable2 = image_stream.available();
                bytesRead2 = image_stream.read(buffer2);
            }
            image_stream.close();

            Log.d("UPLOAD", "Done loading the second buffer");

            out.writeBytes(twoHiphens+boundry+twoHiphens+lineend);
            out.writeBytes(lineend);

            out.flush();
            out.close();



            Log.d("UPLOAD","Response Code = "+conn.getResponseCode());
            String responseMessage = conn.getResponseMessage();
            Log.d("UPLOAD", "Response Message  = "+responseMessage);


            InputStream  in;
            if(conn.getResponseCode() >= 400){
                in = conn.getErrorStream();
                }else{
                    in = conn.getInputStream();
                }
                BufferedReader reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
                StringBuilder response = new StringBuilder();
                char []bytes = new char[512];
                int read ;
                while((read = reader.read(bytes))!=-1){
                    response.append(bytes, 0, read);
                }

                Log.d("UPLOAD", "Response " +response);


                conn.disconnect();

                } catch (MalformedURLException e) {
                    e.printStackTrace();            
                } catch (IOException e) {
                    e.printStackTrace();            
                }           
                return "maine";         
            }


            @Override       protected void onPostExecute(String result) {
                super.onPostExecute(result);            Log.d("UPLOAD", "Upload complete");
            }
        }

1 个答案:

答案 0 :(得分:0)

已解决!!在服务器上发送的文件的MIME类型选择都是错误的!