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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:52:44+00:00 2026-05-23T02:52:44+00:00

I finally made it to upload a file using core java. Here is my

  • 0

I finally made it to upload a file using core java. Here is my code:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;

public class FileUpload {

    public static final String C_EMAIL = "email@gmail.com";
    public static final String C_PASSWORD = "password";

    public static void main(String args[]) {
        try {
            // Send data
            String url = "http://localhost/upload.php";
            File binaryFile = new File("C:\\wamp\\www\\image.png");
            // Just generate some unique random value.
            String boundary = Long.toHexString(System.currentTimeMillis());
            // Line separator required by multipart/form-data.
            String CRLF = "\r\n";
            URLConnection connection = new URL(url).openConnection();
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type",
                "multipart/form-data; boundary=" + boundary);
            PrintWriter writer = null;
            try {
                OutputStream output = connection.getOutputStream();
                // true = autoFlush, important!
                writer = new PrintWriter(
                        new OutputStreamWriter(output, "UTF-8"), true);
                // Send normal param.
                writer.append("--" + boundary).append(CRLF);
                writer.append("Content-Disposition: form-data; name=\"email\"")
                        .append(CRLF);
                writer.append("Content-Type: text/plain; charset=" + "UTF-8")
                        .append(CRLF);
                writer.append(CRLF);
                writer.append(C_EMAIL).append(CRLF).flush();
                // Send binary file.
                writer.append("--" + boundary).append(CRLF);
                writer.append(
                    "Content-Disposition: form-data;"
                        + "name=\"file\"; filename=\"" + binaryFile.getName()
                        + "\"").append(CRLF);
                writer.append("Content-Type: "
                    + URLConnection.guessContentTypeFromName(binaryFile
                            .getName()) + CRLF);
                writer.append("Content-Transfer-Encoding: binary").append(CRLF);
                System.out.println("Content transfer set");
                writer.append(CRLF).flush();
                InputStream input = null;
                try {
                    System.out.println("Entered try");
                    input = new FileInputStream(binaryFile);
                    byte[] buffer = new byte[1024];
                    System.out.println("Before for loop");
                    for (int length = 0; (length = input.read(buffer)) > 0;) {
                        output.write(buffer, 0, length);
                    }
                    output.flush(); // Important! Output cannot be closed.
                    // Close of writer will close output as well.
                    System.out.println("output flushed");
                } finally {
                    if (input != null) try {
                        input.close();
                    } catch (IOException logOrIgnore) {}
                }
                writer.append(CRLF).flush(); // CRLF is important! It indicates
                // end of binary boundary.
                // End of multipart/form-data.
                writer.append("--" + boundary + "--").append(CRLF);
            } finally {
                if (writer != null) writer.close();
            }
            // Get the response
            BufferedReader rd = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                System.out.println(line);
            }
            rd.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

In my PHP script I am able to get the file being uploaded and the ’email’ parameter with its value.
However, I don’t know how can I pass one more additional parameter i.e. “password”.
Can anybody suggest me?

  • 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-23T02:52:45+00:00Added an answer on May 23, 2026 at 2:52 am

    Use either the API provided java.net.URLConnection or the more convenient Apache HttpComponents Client.

    See also:

    • Using java.net.URLConnection to fire and handle HTTP requests – Near the bottom you can find a snippet.
    • HttpClient Examples – At the bottom you can find an “Multipart encoded request entity” example.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file upload script that works [finally] but want to have users
Here's what I've made so far: // somewhere in the page code... <img alt=
I'm finally getting my team to embrace source code management now that we're working
Imgur is a image uploading website who offers an API to upload My code
I finally made a great step by abandoning SVN for Git and loving it.
This morning I finally made my mind and decided to ask you for help.
String a=(Yeahhhh) I have finally made it to the (top); Given above String, there
So, I've finally made the plunge, and have gotten to the state where I'm
As you can guess from the title, the outlining with regions finally made me
Hello there i've finally made a good perfect language system and now am trying

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.