从android上传多部分文件



我正在发送一个多部分请求,我的文件是这样附加的

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

实体是一个MultipartEntityBuilder对象。

我还想设置内容配置。我如何在文件中设置内容配置?请帮助

试试这个:-

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);

相关内容

  • 没有找到相关文章

最新更新