上传内容配置 android

upload with content disposition android

这是我上传 mp3 的代码

    builder= new StringBuilder(); 
    int TIMEOUT_MILLISEC = 10000;  // = 10 seconds
    HttpClient httpclient = new DefaultHttpClient();


    String urlString=base_url+"/api/greetings/phone/"+userExtensionData.getId();

    //System.out.println("urlString=== "+urlString);

    HttpPost httpPost=new HttpPost(urlString);
    try {



        httpPost.addHeader("app_token", userData.getAppToken());
        httpPost.addHeader("user_token",userData.getUserToken());
        httpPost.addHeader("user_ip",userData.getUserIpAddress());



          String BOUNDARY= "--eriksboundry--";

          httpPost.setHeader("Content-Type", "multipart/form-data; boundary="+BOUNDARY);
           MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
           File file= new File(path);
           FileBody bin = new FileBody(file);
           reqEntity.addPart("file",bin);
           reqEntity.addPart("greeting[name]", new StringBody("jdjd"));
           reqEntity.addPart("greeting[description]", new StringBody("dddvd"));




           httpPost.addHeader("Accept-Encoding", "gzip, deflate");

           httpPost.setHeader("Accept", "application/json");

        httpPost.setEntity(reqEntity);      


        HttpResponse response= httpclient.execute(httpPost);

        StatusLine statusLine =response.getStatusLine();

        statusCode=statusLine.getStatusCode();
        //System.out.println("status code === "+statusCode);
       if(statusCode ==200||statusCode ==500 ) {
            if(response!=null){
                HttpEntity entity= response.getEntity();
                InputStream content= entity.getContent();
                BufferedReader reader= new BufferedReader(new InputStreamReader(content));
                String line;
                while((line=reader.readLine())!=null){
                    builder.append(line);
                }

                //System.out.println("submitted  ==  "+builder.toString());

            }
       }

    }catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }catch (ClientProtocolException e) {
        e.printStackTrace();
    }catch (IOException e) {
        e.printStackTrace();
    }

    return null;

我的问题是当我尝试执行此操作时出现 500 错误并且内容处置存在问题。 reqEntity.addPart("greeting[name]", new StringBody("jdjd")); reqEntity.addPart("greeting[description]", new StringBody("dddvd")); 我不知道如何将内容处理添加到多部分实体。 谁能帮帮我。 当我尝试从高级休息客户端上传它的工作 输入图片描述

我无法通过 android 代码执行相同的操作。

MultipartyEntity 已弃用。我建议你选择它的替代方案 MultipartEntityBuilder

这是代码

MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
reqEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
File file= new File(path);
FileBody bin = new FileBody(file);
reqEntity.addPart("file",bin);
reqEntity.addPart("greeting[name]", new StringBody("jdjd"));
reqEntity.addPart("greeting[description]", new StringBody("dddvd"));
HttpEntity entity = reqEntity.build();
httpPost.setEntity(entity);

希望对您有所帮助!