Note that there are some explanatory texts on larger screens.

plurals
  1. POJava posting empty files to remote server
    primarykey
    data
    text
    <p>I am trying to post files as part of POST array with Java to a remote server with this code:</p> <pre><code>public void executeMultiPartRequest(String urlString, File file, String fileName, String login, String password, String project) throws Exception { RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30 * 1000).build(); HttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build(); HttpPost postRequest = new HttpPost (urlString) ; try { //Set various attributes MultipartEntity multiPartEntity = new MultipartEntity () ; multiPartEntity.addPart("login", new StringBody(login != null ? login : "")) ; multiPartEntity.addPart("password", new StringBody(password != null ? password : "")) ; multiPartEntity.addPart("project", new StringBody(project != null ? project : "")) ; multiPartEntity.addPart("fileName", new StringBody(fileName != null ? fileName : file.getName())) ; FileBody fileBody = new FileBody(file, "image/jpeg") ; //Prepare payload multiPartEntity.addPart("attachment", fileBody) ; //Set to request body postRequest.setEntity(multiPartEntity) ; //Send request HttpResponse response = client.execute(postRequest) ; //Verify response if any if (response != null) { System.out.println(response.getStatusLine().getStatusCode()); HttpEntity resEntity = response.getEntity(); System.out.println(response.getStatusLine()); if (resEntity != null) { System.out.println(EntityUtils.toString(resEntity)); } if (resEntity != null) { resEntity.consumeContent(); } client.getConnectionManager().shutdown(); } } catch (Exception ex) { System.err.println(ex); // returns null as the cause is nonexistent or unknown. System.err.println("Cause = " + ex.getCause()); } } </code></pre> <p>It all goes fine when working with <em>localhost</em> and I get my file in the post array. Yet when working with a <em>remote server</em>, <code>$_post['attachment']</code> displays as empty but the files get saved as always identical four bytes of Cyrillic text. I'm a complete newbie with Java and just trying to get this small piece to work in par with my PHP server-side stuff. Why is this happening? Can it be that the client doesn't have the time to properly send the data, or is it more likely that the server is not accepting it? The code processing the requests is similar on <em>localhost</em> and the <em>remote server</em>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload