Hi,
thanks to Bernd - using EntityBuilder its working now.
Solution:
HttpEntity reqEntity = EntityBuilder.create().setFile(fIn).build();
Uploaded file:
--file:
Walze.txt---
line 1: content
line 2: content
--file:
Walze.txt---
Regards, Bastian
-Ursprüngliche Nachricht-
Von: Bernd Eckenfels [mailto:e...@zusammenkunft.net]
Gesendet: Freitag, 27. Januar 2017 09:06
An: httpclient-users@hc.apache.org
Betreff: Re: How to work with HttpPut?
Hello,
You are using a multipart entity as you want to upload a form. Since the server
stores all headers I guess it is expecting a simple entity with the raw content
instead. (I think EntityBuilder.setFile())
Gruss
Bernd
--
http://bernd.eckenfels.net
_
From: Schurr, Bastian
Sent: Freitag, Januar 27, 2017 8:19 AM
Subject: How to work with HttpPut?
To:
Hi,
I have to upload files - text files as well as binary files - to a server using
http put method. I get the URL from the server and upload my sample files to
the server without any failure.
But when I open the uploaded files on the server I find modifications in the
files like follows (original content in green):
--file:
Walze.txt---
--359OQxCzKReuMiQUCDjZkKneprgsPnZApeFpvoPF
Content-Disposition: form-data; name="Walze.txt"; filename="Walze.txt"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
line 1: content
line 2: content
--359OQxCzKReuMiQUCDjZkKneprgsPnZApeFpvoPF--
--file:
Walze.txt---
Here is my code:
public List put(File fIn, String sUrl) throws IllegalArgumentException,
ClientProtocolException, IOException {
if (fIn != null && sUrl != null && !sUrl.isEmpty()) {
CloseableHttpClient client =
HttpClients.createDefault();
try {
HttpPut request = new
HttpPut(sUrl);
FileBody file = new
FileBody(fIn);
HttpEntity reqEntity =
MultipartEntityBuilder.create().addPart(fIn.getName(), file).build();
request.setEntity(reqEntity);
CloseableHttpResponse response =
client.execute(request);
List vLines =
this.readResponse(response);
this.handleResponse(response);
return vLines;
} finally {
try {
client.close();
} catch (IOException eIgnore) {
}
}
}
}
private List readResponse(CloseableHttpResponse response) throws
IOException {
List vLines = new ArrayList();
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
BufferedReader reader = new BufferedReader(new
InputStreamReader(resEntity.getContent()));
String s;
while (null != (s = reader.readLine())) {
if (!s.isEmpty()) {
vLines.add(s);
}
}
}
return vLines;
}
private void handleResponse(CloseableHttpResponse response) throws IOException {
Logger.getLogger(this.getClass()).debug("Response status line
[" + response.getStatusLine() + "]");
Logger.getLogger(this.getClass()).debug("Response status code
[" + response.getStatusLine().getStatusCode() + "]");
Logger.getLogger(this.getClass()).debug("Response status phrase
[" + response.getStatusLine().getReasonPhrase() + "]");
Header[] vHeaders = response.getAllHeaders();
for (Header header : vHeaders) {
String sName = header.getName();
String sValue = header.getValue();
Logger.getLogger(this.getClass()).debug("Response header " + sName + " [" +
sValue + "]");
}
HttpEntity resEntity = response.getEntity();