Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5951867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:35:57+00:00 2026-05-22T17:35:57+00:00

I am trying to upload an image to a server along with some JSON

  • 0

I am trying to upload an image to a server along with some JSON data that is collected from a form.

The server has authentication.

METHOD: post

HEADERS:

Authorization   Basic d2Vic2VydmljZTpyM05hdTE3Rw==

Content-Type    multipart/form-data;boundary=xxxxxxxx

BODY:

--xxxxxxxx

Content-Disposition: form-data; name="jsonFile"
Content-Type: application/json

{"model":"Premium","deviceLongitude":4.79337638,"pseudo":"nickname","deviceLatitude":45.7671507,"year":"2005","email":"email@mail.com","deviceLocale":"fr_FR","title":"my picture"}

--xxxxxxxx

Content-Disposition: form-data; name="imgName"
Content-Type: image/jpeg

//Image data array

/9j/4AAQSkZJRgABAQAAAQABAAD/4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAAB
AAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAB4KAD
AAQAAAABAAACgAAAAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQH/wAARCAKAAeADAREAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAA
AAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF
--xxxxxxxx
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:35:58+00:00Added an answer on May 22, 2026 at 5:35 pm
    /**
     * This utility function will upload the file to the Url
     * * @param filePath - absolute path of the file to be uploaded
     * @param postUrl  - Remote Url where the file need to be posted
     * @param contentType - content-type of the uploaded file
     * @throws Exception
     */
    public static void postFile(String filePath, String postUrl,
            String pictureTitleStr, String pseudoTextStr)
            throws Exception {
    
        String url = postUrl;
        HttpURLConnection conn = null;
        final String CrLf = "\r\n";
        JSONObject json = new JSONObject();
        int bytesRead = 0;
    
    
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "xxxxxxxx";
        String EndBoundary = "";
        int maxBufferSize = 1 * 1024 * 1024;
    
        HttpResponse response = null;
    
      // Having HttpClient to respond to both HTTP and HTTPS url connection by accepting the urls along with keystore / trust certificates 
    
        try {
            KeyStore trustStore = KeyStore.getInstance(KeyStore
                    .getDefaultType());
            trustStore.load(null, null);
    
            SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            HttpParams params = new BasicHttpParams();
            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
            HttpProtocolParams.setUserAgent(params, "YourAppName/1.1");
            HttpConnectionParams.setStaleCheckingEnabled(params, false);
            HttpConnectionParams.setConnectionTimeout(params, 20 * 1000);
            HttpConnectionParams.setSoTimeout(params, 20 * 1000);
            HttpConnectionParams.setSocketBufferSize(params, 8192);
            HttpClientParams.setRedirecting(params, false);
    
            SchemeRegistry registry = new SchemeRegistry();
            registry.register(new Scheme("http", PlainSocketFactory
                    .getSocketFactory(), 80));
            registry.register(new Scheme("https", sf, 443));
    
            ClientConnectionManager ccm = new ThreadSafeClientConnManager(
                    params, registry);
    
            mHttpClient = new DefaultHttpClient(ccm, params);
    
    
    
        } catch (Exception e) {
    
        }
    
        String base64EncodedCredentials = Base64.encodeToString((userName + ":" + password).getBytes("US-ASCII"),
                Base64.DEFAULT);
        System.out.println("Encoded Credit " + base64EncodedCredentials);
    
                json.put("pseudo", pseudoTextStr);
                json.put("title", pictureTitleStr);
    
               String jsonStr = json.toString(); 
     //   System.out.println("JSON VALUE  " + jsonStr);
    
        URL url2 = new URL(postUrl);
    
    
    
        Bitmap bm = BitmapFactory.decodeFile(filePath);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 25, baos); // bm is the bitmap object
        byte[] b = baos.toByteArray();
    
        String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
    
    
        String str = twoHyphens + boundary + lineEnd;
        String str2 = "Content-Disposition: form-data; name=\"jsonFile\"";
        String str3 = "Content-Type: application/json";
        String str4 = "Content-Disposition: form-data; name=\"imgName\"";
        String str5 = "Content-Type: image/jpeg";
        String str6 = twoHyphens + boundary + twoHyphens;
    
    
    
        String StrTotal = str + str2 + "\r\n" + str3 + "\r\n" +"\r\n" + jsonStr + "\r\n" + str
                + str4 + "\r\n" + str5 + "\r\n"+"\r\n"+ encodedImage + "\r\n" + str6;
    
        //System.out.print("Multipart request string is "+StrTotal);
    
     HttpPost post = new HttpPost(postUrl);
    
    
    post.addHeader(BasicScheme.authenticate(new UsernamePasswordCredentials(
                    userName, password), "UTF-8", false));
    post.addHeader("Content-Type","multipart/form-data;boundary="+boundary);
    // System.out.println("Sending Post proxy request: " + post);
    
     StringEntity se = new StringEntity(StrTotal);
     se.setContentEncoding("UTF-8");
     post.setEntity(se);
     response = mHttpClient.execute(post);
    
    /* Checking response */
    
    statusCode = response.getStatusLine().getStatusCode();
    System.out.println("Http Execute finish " + statusCode);
    
    HttpEntity entity = response.getEntity();
    String getResponseText = entity.toString(); // EntityUtils.toString(entity);
    System.out.println(" Post Response Text from Server : "
            + getResponseText);
    
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm trying to upload some data from an ActiveX control to a webpage. The
I am trying to upload an image to the server. I hava a form
I'm trying to upload an image from android to a PHP server by using
I'm trying to upload an image to my site through a form, however it's
I am trying to upload an image to server using iphone . I haven't
I am trying to upload an image to a directory on a server. i'm
I am trying to upload a file or stream of data to our web
I'm trying to write some PHP to upload a file to a folder on
I am trying to find out how to upload a file from a web
I am trying to do a file upload from gwt-ext without bringing up the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.