hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Schurr, Bastian" <B.Sch...@cenit.de>
Subject AW: How to work with HttpPut?
Date Tue, 31 Jan 2017 06:34:59 GMT
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:ecki@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 <b.schurr@cenit.de>
Sent: Freitag, Januar 27, 2017 8:19 AM
Subject: How to work with HttpPut?
To:  <httpclient-users@hc.apache.org>


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<String> 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<String> vLines = this.readResponse(response);

                                               this.handleResponse(response);

                                               return vLines;
                               } finally {
                                               try {
                                                               client.close();
                                               } catch (IOException eIgnore) {
                                               }
                               }
                }
}

private List<String> readResponse(CloseableHttpResponse response) throws IOException
{
                List<String> vLines = new ArrayList<String>();

                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();
                if (resEntity != null) {
                               Logger.getLogger(this.getClass()).debug("Response content type
[" + resEntity.getContentType() + "]");
                               Logger.getLogger(this.getClass()).debug("Response content length
[" + resEntity.getContentLength() + "]");

                               EntityUtils.consume(resEntity);
                }

                try {
                               response.close();
                } catch (IOException eIgnore) {
                }
}


Can anyone please tell me what is missing in my code or what can be the failure?

Thanks and Regards

Bastian


CENIT AG, Industriestrasse 52-54, 70565 Stuttgart, Tel.: +49 711 7825-30, Fax: +49 711 7825-4000,
Internet: www.cenit.com
Geschaeftsstellen: Berlin, Frankfurt, Hamburg, Hannover, Muenchen, Oelsnitz, Ratingen, Saarbruecken
Vorstandsmitglieder: Kurt Bengel, Matthias Schmidt
Aufsichtsratsmitglieder: Andreas Schmidt (Vorsitzender des Aufsichtsrats), Hubert Leypoldt,
Andreas Karrer
Bankverbindungen:
Deutsche Bank (BLZ 600 700 70) Kto. 1661 040 IBAN : DE85 6007 0070 0166 1040 00 SWIFT-CODE
: DEUTDESS,
Commerzbank (BLZ 600 400 71) Kto. 532 015 500 IBAN : DE83 6004 0071 0532 0155 00 SWIFT-Code
: COBADEFF600,
Registergericht: Amtsgericht Stuttgart
Handelsregister: HRB Nr. 19117
Umsatzsteuer: ID-Nr. DE 147 862 777


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message