List of usage examples for org.apache.commons.httpclient.methods.multipart MultipartEntity MultipartEntity
public MultipartEntity(Part[] parts, HttpParams params)
From source file:net.exclaimindustries.geohashdroid.wiki.WikiUtils.java
/** Uploads an image to the wiki @param httpclient an active HTTP session, wiki login has to have happened before. @param filename the name of the new image file @param description the description of the image. An initial description will be used as page content for the image's wiki page @param formfields a formfields hash as modified by getWikiPage containing an edittoken we can use (see the MediaWiki API for reasons why) @param data a ByteArray containing the raw image data (assuming jpeg encoding, currently). *///ww w . ja v a2 s .c om public static void putWikiImage(HttpClient httpclient, String filename, String description, HashMap<String, String> formfields, byte[] data) throws Exception { if (!formfields.containsKey("token")) { throw new WikiException(R.string.wiki_error_unknown); } HttpPost httppost = new HttpPost(WIKI_API_URL); // First, we need an edit token. Let's get one. ArrayList<NameValuePair> tnvps = new ArrayList<NameValuePair>(); tnvps.add(new BasicNameValuePair("action", "query")); tnvps.add(new BasicNameValuePair("prop", "info")); tnvps.add(new BasicNameValuePair("intoken", "edit")); tnvps.add(new BasicNameValuePair("titles", "UPLOAD_AN_IMAGE")); tnvps.add(new BasicNameValuePair("format", "xml")); httppost.setEntity(new UrlEncodedFormEntity(tnvps, "utf-8")); Document response = getHttpDocument(httpclient, httppost); Element root = response.getDocumentElement(); // Hopefully, a token exists. If not, a problem exists. String token; Element page; try { page = DOMUtil.getFirstElement(root, "page"); token = DOMUtil.getSimpleAttributeText(page, "edittoken"); } catch (Exception e) { throw new WikiException(R.string.wiki_error_xml); } // TOKEN GET! Now we've got us enough to get our upload on! Part[] nvps = new Part[] { new StringPart("action", "upload", "utf-8"), new StringPart("filename", filename, "utf-8"), new StringPart("comment", description, "utf-8"), new StringPart("watch", "true", "utf-8"), new StringPart("ignorewarnings", "true", "utf-8"), new StringPart("token", token, "utf-8"), new StringPart("format", "xml", "utf-8"), new FilePart("file", new ByteArrayPartSource(filename, data), "image/jpeg", "utf-8"), }; httppost.setEntity(new MultipartEntity(nvps, httppost.getParams())); response = getHttpDocument(httpclient, httppost); root = response.getDocumentElement(); // First, check for errors. if (doesResponseHaveError(root)) { throw new WikiException(getErrorTextId(findErrorCode(root))); } }