0

Hi I am uploadingfile to php server.For this we require to send string parameter .I am using following code for uploading.So is there any tutorial that set parameter or any explanation that describes setting .thanks url = new URL(httpPath);

    connection = (HttpURLConnection) url.openConnection();

    // Allow Inputs & Outputs
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);

    // Enable POST method
    connection.setRequestMethod("POST");

    connection.setRequestProperty("Connection", "Keep-Alive");
    connection.setRequestProperty("Content-Type",
            "multipart/form-data;boundary=" + boundary);

    outputStream = new DataOutputStream(connection.getOutputStream());
    outputStream.writeBytes(twoHyphens + boundary + lineEnd);
1

1 Answer 1

1

Don't reinvent the wheel. Use Apache HttpClient. Read this javadoc page for an example of a post containing a file upload and a string parameter:

  File f = new File("/path/fileToUpload.txt");
  PostMethod filePost = new PostMethod("http://host/some_path");
  Part[] parts = {
      new StringPart("param_name", "value"),
      new FilePart(f.getName(), f)
  };
  filePost.setRequestEntity(
      new MultipartRequestEntity(parts, filePost.getParams())
      );
  HttpClient client = new HttpClient();
  int status = client.executeMethod(filePost);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for quick reply.I want to used in android .Does it works. thanks
You should have tagged your question with android. Android bundles the apache HttpClient, but without this class. Nevertheless, it seems that it's easy enough to add it. Read the following Google groups post: groups.google.com/group/android-developers/msg/1ea124b384da9dc2. You'll need to use the mime4j and httpmime libraries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.