0

I`m using the following code to submit a form:

URL url = new URL(submit_url);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();

connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=*****");
connection.setRequestProperty("Cookie", cookie);            

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
bw.write(query);
bw.write("\r\n");
bw.flush();
bw.close();

But I need to submit a file, too. The file input name is: file and my file is located at: d:/images/test.gif Please help me to submit the image, too. The query is a string: name=username&title=title

James Goodwin
  • 7,041
  • 3
  • 26
  • 40
coolboycsaba
  • 183
  • 6
  • 13
  • You can find an example near the bottom of this answer: http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests – BalusC Nov 24 '11 at 21:59

1 Answers1

0

Create a DataOutputStream from your HttpUrlConnection, then write whatever bytes you want to upload to the server:

DataOutputStream dataOutputStream = 
        new DataOutputStream(connection.getOutputStream());
dataOutputStream.writeBytes(bytes);
dataOutputStream.close();
James Goodwin
  • 7,041
  • 3
  • 26
  • 40