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

  • SEARCH
  • Home
  • 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 8888477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:03:00+00:00 2026-06-14T22:03:00+00:00

I have used Java to post information to a form before, but I have

  • 0

I have used Java to post information to a form before, but I have never done it with a file. I am testing this process with an image and text file, but apparently I have to change the way that I am doing it. The current way I am doing it (shown below) does not work and I am not completely sure if I can still use HttpClient.

The params part only accepts type string. I have a form that I am uploading files to a server with. The site I use for our CMS doesnt allow a direct connection so I have to upload files automatically with a form.

public static void main(String[] args) throws IOException {
    File testText = new File("C://xxx/test.txt");
    File testPicture = new File("C://xxx/test.jpg");

    HttpClient httpClient = new HttpClient();
    PostMethod postMethod = new PostMethod("xxxx");
    postMethod.addParameter("test", testText);

    try {
        httpClient.executeMethod(postMethod);
    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
  • 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-14T22:03:02+00:00Added an answer on June 14, 2026 at 10:03 pm

    Use setRequestEntity method to directly send file.

    FileRequestEntity fre = new FileRequestEntity(new File("C://xxx/test.txt"), "text/plain");
    post.setRequestEntity(fre);
    postMethod.setRequestEntity(fre);
    

    For sending as form-data, use MultipartRequestEntity

    File f = new File("C://xxx/test.txt");
    Part[] parts = {
        new FilePart("test", f)
    };
    postMethod.setRequestEntity(
        new MultipartRequestEntity(parts, postMethod.getParams())
        );
    

    Ref: https://stackoverflow.com/a/2092508/324900

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that writes information to file. This information is used post-execution
I have used java mail(com.sun.mail.smtp) from my struts project. Mail is sent successfully but
Once I have programmed GUI with Java and have used Form Layouts. Form layout
I am new to Java (and Eclipse) but I have used .NET (and Visual
I have a jar library. I used Java Class File Editor (CE) to modify
I have prefer this post for example but I got the error at adding
Before I post this I looked at some past questions on this exceptions but
I have used the Java Wireless Toolkit Stub Generator to create a bunch of
Here is the piece of code that I have used for Java 5.0 TreeSet<Integer>
If you have used any decent java or .net IDE you can see 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.