return new String(content, Constants.HTTP_ENCODEING);
}
public static String doPostMultipart(String strUrl, Map<String, String> params, Map<String, String> fileUrlMaps) throws IOException {
URLFetchService ufs = URLFetchServiceFactory.getURLFetchService();
java.net.URL url = new java.net.URL(strUrl);
HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST, FetchOptions.Builder.withDeadline(TIMEOUT_SECONDS).allowTruncate());
String boundary = makeBoundary();
request.setHeader(new HTTPHeader("Content-Type", "multipart/form-data; boundary=" + boundary));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// form-data
for (Map.Entry<String, String> formData : params.entrySet()) {
write(baos, "--" + boundary + "\r\n");
writeFormData(baos, formData.getKey(), formData.getValue());
}
// multipart
for (Map.Entry<String, String> fileUrlMap : fileUrlMaps.entrySet()) {
log.info("fetch binary file: " + fileUrlMap.getValue());
HTTPRequest donwloadFilerequest = new HTTPRequest(new java.net.URL(fileUrlMap.getValue()), HTTPMethod.GET, FetchOptions.Builder.withDeadline(TIMEOUT_SECONDS));
HTTPResponse donwloadFileResponse = ufs.fetch(donwloadFilerequest);
byte[] binaryContent = donwloadFileResponse.getContent();
String contentType = null;
for (HTTPHeader header : donwloadFileResponse.getHeaders()) {
if (header.getName().toLowerCase().equals("content-type")) {
contentType = header.getValue();
break;
}
}
// file
write(baos, "--" + boundary + "\r\n");
writeMultipart(baos, fileUrlMap.getKey(), StringUtils.getFileName(fileUrlMap.getValue()), contentType, binaryContent);
}
write(baos, "--" + boundary + "--\r\n");
request.setPayload(baos.toByteArray());
HTTPResponse response = ufs.fetch(request);
byte[] content = response.getContent();
return new String(content, Constants.HTTP_ENCODEING);
}