Убедитесь, что вы используете POST-форму, состоящую из нескольких частей, как в примере ниже:
final String CHARSET = "ISO-8859-1";
final String CRLF = "\r\n";
String formFieldName = "uploadfile";
String fileName = "upload.jpeg";
String fileContentType = "image/jpeg";
URL url = new URL("http://localhost");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true); // if input is expected
String boundary = "--------------------"+System.currentTimeMillis();
StringBuilder postData = new StringBuilder();
postData.append("--").append(boundary).append(CRLF);
postData.append("Content-Disposition: form-data; name=\"").append(formFieldName).append("\"; filename=\"").append(fileName).append("\"").append(CRLF);
postData.append("Content-Type: ").append(fileContentType).append(CRLF);
postData.append(CRLF).append(new String(bytes, CHARSET)).append(CRLF);
postData.append("--").append(boundary).append("--").append(CRLF);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary="+boundary);
connection.setRequestProperty("Content-Length", Integer.toString(postData.length()));
connection.setFixedLengthStreamingMode(postData.length());
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), CHARSET);
out.write(postData.toString());
out.close();
assert(connection.getResponseCode() == HttpURLConnection.HTTP_OK);
// if input is expected
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), CHARSET));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
Cheers, Max