Not able to upload image to server using Httprequest library

こ雲淡風輕ζ 提交于 2019-12-02 09:47:54

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();
            }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!