You need to send a POST request containing several parameters and a file.

String sendfile (String filename, String filepath) throws IOException { String boundary = Long.toHexString(System.currentTimeMillis()); String charset = "UTF-8"; URL url = new URL("..."); File file = new File(filepath); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("POST"); con.setUseCaches(false); con.setDoOutput(true); con.setDoInput(true); con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); OutputStream os = con.getOutputStream(); PrintWriter writer = new PrintWriter(new OutputStreamWriter(os, charset), true); writer.println("--" + boundary); writer.println("Content-Disposition: form-data; name=\"key\""); writer.println(); writer.println(key); writer.println("--" + boundary); writer.println("Content-Disposition: form-data; name=\"client_id\""); writer.println(); writer.println(client_id); writer.println("--" + boundary); writer.println("Content-Disposition: form-data; name=\"direction_id\""); writer.println(); writer.println(direction_id); writer.println("--" + boundary); writer.println("Content-Disposition: form-data; name=\"client_files\"; filename=\"" + filename + "\""); writer.println("Content-Type: image/jpg"); writer.println(); BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file))); for (String line; (line = reader.readLine()) != null; ) writer.println(line); writer.println("--" + boundary + "--"); os.flush(); os.close(); BufferedReader in = new BufferedReader( new InputStreamReader( con.getInputStream())); return in.readLine(); } 

The problem is somewhere in the last block, because the server sees the parameters, but it responds that the file was not found. I tried to comment out the block of sending one parameter, but an authorization error came.

Error code does not issue.

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

To build multipart / form-data it is better to use the already prepared MultipartEntityBuilder from the org.apache.httpcomponents:httpmime package org.apache.httpcomponents:httpmime :

 MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody("key", key); builder.addTextBody("client_id", client_id ); builder.addTextBody("direction_id", direction_id); ContentType fileContentType = ContentType.create("image/jpeg"); String fileName = file.getName(); builder.addBinaryBody("client_files", file, fileContentType, fileName); HttpEntity entity = builder.build(); 

It takes less effort to write and maintain such a code, and the probability of an imperceptible error in it is also much less.

The resulting HttpEntity can be written to any OutputStream:

 entity.writeTo(outputStream); 

And even better, immediately send a POST request to the server using HttpClient 'from org.apache.httpcomponents:httpclient :

 HttpPost request = new HttpPost(uri); request.setEntity(entity); HttpClient client = HttpClients.createDefault(); client.execute(request); 
  • Thanks, helped) - Alexey Igumnov