Android的多部分文件上传

时间:2015-10-26 11:54:07

标签: android multipartentity

我发送了一个多部分请求,我的文件就像这样附加了

    entity.addPart(entry.getValue().getName(), new FileBody(entry.getValue(), entry.getValue().getName(), "image/jpeg", "ISO-8859-1"));

entity是一个MultipartEntityBuilder对象。

我也想设置内容处理。如何设置内容配置文件?请帮忙

1 个答案:

答案 0 :(得分:0)

试试这个: -

InputStream inputStream = new FileInputStream(new File(String.valueOf(params[0])));

//*** CONVERT INPUTSTREAM TO BYTE ARRAY
byte[] data = this.convertToByteArray(inputStream);

            HttpClient httpClient = new DefaultHttpClient();


HttpPost httpPost = new HttpPost(UPLOAD_SERVER_URI);

String boundary = "----WebKitFormBoundarypJjqFVXFVatNaatW";

httpPost.setHeader("Accept", "*/*");
httpPost.setHeader("Content-type", "multipart/form-data; boundary="+boundary);

//File Data
InputStreamBody inputStreamBody = new InputStreamBody(new ByteArrayInputStream(data), "image/jpeg");

    CustomMultiPartEntity multipartEntity = new CustomMultiPartEntity();

            // SET UPLOAD LISTENER
            multipartEntity.setUploadProgressListener(this);

            //*** ADD THE FILE
            multipartEntity.addPart("imagecode", inputStreamBody);

            /** Add String Data */
            entity.addPart("companycode", new StringBody(companycode));

            httpPost.setEntity(multipartEntity);

            // EXECUTE HTTPPOST
            HttpResponse httpResponse = httpClient.execute(httpPost);

            // THE RESPONSE FROM SERVER
            String stringResponse = EntityUtils.toString(httpResponse.getEntity());

            // DISPLAY RESPONSE OF THE SERVER
            Log.d("data from server", stringResponse);