Not able to upload image to server using Httprequest library

前端 未结 1 1115
北荒
北荒 2021-01-26 11:25

I have created an app in which user select an image from gallery and that image is uploaded to the server.I tried doing it using Httprequest Library.But i m not being able to up

相关标签:
1条回答
  • 2021-01-26 12:02

    Try This code and this library add httpmime-4.1-beta1.jar :

    String url = "Your Url";
    
    
                HttpClient client = new DefaultHttpClient();
                client.getParams().setParameter(
                        CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
                HttpPost postMethod = new HttpPost(url);
    
    
                MultipartEntity entity = new MultipartEntity();
    
                  entity.addPart("fname", new StringBody("xyz"));
    
                try {
    
                        File file1 = new File("Your image Path");
                        FileBody bin1 = new FileBody(file1, "images/jpeg");
    
                        entity.addPart("file", bin1);
    
                    postMethod.setEntity(entity);
                    HttpResponse response;
                    response = client.execute(postMethod);
    
                    String result = EntityUtils.toString(response.getEntity());
    
                }
    
                catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
    0 讨论(0)
提交回复
热议问题