public static boolean SendPost(String url, String filepath, String docname,
String docnumber, String docdate, String er[]) {
boolean result = false;
XPathFactory xpathFactory = XPathFactory. newInstance();
XPath xpath = xpathFactory.newXPath();
try {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpPost httppost = new HttpPost(url);
// httppost .setHeader("Content-Type",
// "multipart /form-data; charset=UTF-8");
FileBody bin = new FileBody(new File(filepath));
StringBody saction = new StringBody("document-add" ,
ContentType. TEXT_PLAIN);
StringBody sdocname = new StringBody(docname,
ContentType. TEXT_PLAIN.withCharset(Charset
. forName("UTF-8")));
StringBody sdocnumber = new StringBody(docnumber,
ContentType. TEXT_PLAIN.withCharset(Charset
. forName("UTF-8")));
StringBody sdocdate = new StringBody(docdate,
ContentType. TEXT_PLAIN);
HttpEntity reqEntity = MultipartEntityBuilder.create().addPart(
"file", bin).addPart("action" , saction).addPart("name" ,
sdocname).addPart( "number", sdocnumber).addPart("date" ,
sdocdate)
// .setCharset(Charset.forName("UTF-8"))
.build();
httppost.setEntity(reqEntity);
System. out.println("executing request "
+ httppost.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httppost);
try {
System. out
.println( "----------------------------------------" );
System. out
.println(response.getStatusLine().getStatusCode());
Integer statuscode = response.getStatusLine()
.getStatusCode();
if (statuscode != 200) {
System. out.println("ошибка -сервер вернул код "
+ statuscode);
er[0] = "ошибка -сервер вернул код " + statuscode;
result = false;
}
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
System. out.println("Response content length: "
+ resEntity.getContentLength());
String xml = EntityUtils.toString(resEntity, "UTF-8");
System. out.println(xml);
InputSource source = new InputSource(new StringReader(
xml));
DocumentBuilderFactory dbf = DocumentBuilderFactory
. newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document document = db.parse(source);
String code = xpath.evaluate("/result/code" , document);
String msg = xpath
.evaluate( "/result/message", document);
String id = xpath.evaluate("/result/document_id" ,
document);
System. out.println("code=" + code);
System. out.println("message=" + msg);
System. out.println("id=" + id);
if (code.equals("ok" )) {
er[1] = id;
result = true;
} else {
er[0] = "сервер вернул " + msg;
result = false;
}
}
EntityUtils. consume(resEntity);
} finally {
response.close();
}
} finally {
httpclient.close();
}
} catch (ClientProtocolException ex) {
er[0] = "ошибка протокола или адреса " + url;
System. out.println("ошибка протокола или адреса " + url);
System. out.println(ex.toString());
result = false;
} catch (UnknownHostException ex) {
er[0] = "ошибка в адресе " + url;
System. out.println("ошибка в адресе " + url);
System. out.println(ex.toString());
result = false;
} catch (Exception e) {
er[0] = "неизвестная ошибка при отправке";
e.printStackTrace();
result = false;
} finally {
}
return result;
}