how to upload the image into server

前端 未结 2 548
Happy的楠姐
Happy的楠姐 2021-01-14 21:41

i tried to upload the image to the server for two days but i could not post the image .the coding is compiled and run sucessfully but the imag is not write into the server.<

相关标签:
2条回答
  • 2021-01-14 22:17
    public boolean fileUpload(Map<String , String> params, ByteArrayOutputStream file, String link) throws Throwable{
    
    
        Account user = Util.getAccount(getApplicationContext());
    
        try{
    
            HttpClient httpClient = new DefaultHttpClient();
    
            HttpPost postRequest = new HttpPost(link);
    
            MultipartEntity multipartContent = new MultipartEntity();
    
            if (params != null && !params.isEmpty()) {
                for (Map.Entry<String , String> entry : params.entrySet()) {
                    multipartContent.addPart(entry.getKey(),new StringBody(entry.getValue(),Charset.forName(HTTP.UTF_8)));
                }
            }
    
            byte[] data = file.toByteArray();
    
            ByteArrayBody img = new ByteArrayBody(data, "capture.jpg");
            multipartContent.addPart("image",img);
            postRequest.setEntity(multipartContent);
            HttpResponse res = httpClient.execute(postRequest);
            res.getEntity().getContent().close();
    
            return true;
    
        }catch(Throwable e){
            throw e;
        }
    
    }
    
    0 讨论(0)
  • 2021-01-14 22:27

    here is a nice article, http://blog.sptechnolab.com/2011/03/09/android/android-upload-image-to-server/. It contains an example that will upload an image and write it at server side. It works.

    0 讨论(0)
提交回复
热议问题