1

I've got a problem sending file with POST Request. If fileName has brackets , then server doesn't receive file at all, otherwise server receive file with name I inserted.

I've already tried to use
- volley library
- appach httpmime different versions
- Android Asynchronous Http Client
and I experience the same result.

Here is code I use

       String fileName = "link[image]";//name of the parameter         
       HttpContext localContext = new BasicHttpContext();
       MultipartEntityBuilder builder = MultipartEntityBuilder.create();
       builder.setLaxMode();
       builder.setCharset(Charset.forName("UTF-8"));
       builder.addBinaryBody(fileName, image, ContentType.APPLICATION_OCTET_STREAM, image.getName());//image - it's a File
       HttpClient client = new DefaultHttpClient();
       HttpPost post = new HttpPost(URL);
       HttpEntity entity = builder.build();
       post.setEntity(entity);
       HttpResponse response = client.execute(post, localContext);
       HttpEntity httpEntity = response.getEntity();
       String result = EntityUtils.toString(httpEntity);
PaulKh
  • 161
  • 1
  • 6

1 Answers1

0

Try this way

String fileName = "link[image]";
        HttpContext localContext = new BasicHttpContext();
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        entity.addPart("image", new FileBody(new File(fileName)),"image/jpg");
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(URL);
        HttpEntity entity = builder.build();
        post.setEntity(entity);
        HttpResponse response = client.execute(post, localContext);
        HttpEntity httpEntity = response.getEntity();
        String result = EntityUtils.toString(httpEntity);
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
  • It won't work. MultipartEntity is deprecated and doesn't have such Constructor. _image_ - is an object of type File. _filename_ - name of file parameter in request. – PaulKh Apr 17 '14 at 12:24