@Override public boolean postTurnSummary( final GameData gameData, final String turnSummary, final String player, final int round) { try { final List<Part> parts = new ArrayList<>(); parts.add(createStringPart("siteid", m_siteId)); if (gameData != null) { parts.add(createStringPart("production", getProductionData(gameData))); } parts.add(createStringPart("gamename", m_gameName)); parts.add(createStringPart("player", player)); parts.add(createStringPart("summary", turnSummary)); parts.add(createStringPart("round", "" + round)); parts.add(createStringPart("sendmail", m_mailSaveGame ? "true" : "false")); if (m_saveGameFile != null) { final FilePart part = new FilePart("userfile", m_saveGameFileName, m_saveGameFile); part.setContentType("application/octet-stream"); parts.add(part); } m_serverMessage = executePost(m_host, "upload.php", parts); if (!m_serverMessage.toLowerCase().contains("success")) { System.out.println("Unknown error, site response: " + m_serverMessage); return false; } } catch (final Exception e) { m_serverMessage = e.getMessage(); ClientLogger.logQuietly(e); return false; } return true; }
public static String post(String path, ChatMessageBean cm, FormFile file) throws Exception { MultipartPostMethod filePost = new MultipartPostMethod(path); FilePart cbFile = new FilePart(file.getFileName(), file.getFile()); cbFile.setContentType("audio/" + file.getParameterName()); filePost.addParameter("message", new JSONObject().toJSONString(cm)); filePost.addParameter("filename", file.getFileName()); filePost.addParameter("uploadFiles", file.getFileName(), file.getFile()); filePost.addPart(cbFile); filePost .getParams() .setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); HttpClient client = new HttpClient(); int statusInt = client.executeMethod(filePost); System.out.println("statusInt:" + statusInt); return filePost.getResponseBodyAsString(); }
/** * This creates a HttpMethod with the message as its payload and image attachment. The message * should be a properly formatted JSON String (No validation is done on this). * * <p>The message can be easily created using the {@link #getJsonPayload(Message)} method. * * @param uri The full URI which we will post to * @param message A properly formatted JSON message. UTF-8 is expected * @param image A complete instance of ImageAttachment object * @throws IOException */ public HttpMethod getJsonPostForMultipartRequestEntity( String uri, String message, ImageAttachment image) throws IOException { PostMethod post = new PostMethod(uri); StringPart bodyPart = new StringPart("json", message); bodyPart.setContentType("application/json"); FilePart filePart = new FilePart("feedItemFileUpload", image.retrieveObjectFile()); filePart.setContentType(image.retrieveContentType()); Part[] parts = { bodyPart, filePart, }; post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams())); return post; }