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 8254357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:09:45+00:00 2026-06-08T01:09:45+00:00

I have an html form that looks something like this: <div class=field> <input id=product_name

  • 0

I have an html form that looks something like this:

<div class="field>
  <input id="product_name" name="product[name]" size="30" type="text"/>
</div>

<div class="field>
  <input id="product_picture" name="product[picture]" size="30" type="file"/>
</div>

I want to write a Java module that automates the creation of product. Here is what I already have:

HttpHost host = new HttpHost("localhost", 3000, "http");
HttpPost httpPost = new HttpPost("/products");
List<BasicNameValuePair> data = new ArrayList<BasicNameValuePair>();
data.add(new BasicNameValuePair("product[name]", "Product1"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(data, "UTF-8");
httpPost.setEntity(entity);
HttpResponse postResponse = httpClient.execute(host, httpPost); 

This works fine, it is able to create new product with name “Product1”. But I don’t know how to handle the upload part. I would like something looks like this:

data.add(new BasicNameValuePair("product[name]", "Product1"));

but instead of “Product1” it is a File. I read the documentation of HttpClient it’s said that there’s only string.

Does anyone know how to handle the uploading part ?

  • 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-06-08T01:09:47+00:00Added an answer on June 8, 2026 at 1:09 am

    Dependencies:

    <dependency>
     <groupid>org.apache.httpcomponents</groupid>
     <artifactid>httpclient</artifactid>
     <version>4.0.1</version>
    </dependency>
    
    <dependency>
     <groupid>org.apache.httpcomponents</groupid>
     <artifactid>httpmime</artifactid>
     <version>4.0.1</version>
    </dependency>
    

    Code:[Tricky part is use of MultipartEntity ]

    HttpClient client = new DefaultHttpClient();
    client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);
    HttpPost post = new HttpPost( url );
    MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE );
    // For File parameters
    entity.addPart( paramName, new FileBody((( File ) paramValue ), "application/zip" ));
    // For usual String parameters
    entity.addPart( paramName, new StringBody( paramValue.toString(), "text/plain", Charset.forName( "UTF-8" )));
    post.setEntity( entity );
    // Here we go!
    String response = EntityUtils.toString( client.execute( post ).getEntity(), "UTF-8" );
    client.getConnectionManager().shutdown();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple html form that is structured something like this: <form name=test
I have an HTML form that needs multiple submit buttons, like this: <input type=submit
We have some html that looks like this: <form id=MyUserControl runat=server method=post> <script language=javascript
I have html in my form that looks like this. First, is this code
I have a simple html form that looks like the following <form action=search.php method=get>
I have a search form that looks like this: The code behind the form
I have a nested form in a rails view that is called like this
I'm generating an html file which looks like: <tr id=ID001 property1=PROPERTY001><td><input type=checkbox name=row_checkbox_ID001></td><td>...</td><td>...</td></tr> <tr
I have a servlet that calls MySQL database. The deletePage method looks like this:
I have a table that looks like the following. <tr> <td>Link Name</td> <td><a href=#

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.