How to upload image to MediaWiki site from android
I'm trying to upload an image to a custom MediaWiki site from an android phone. Unfortunately, the image I am trying to upload isn't working. This is the function I'm using to send a multipart request:
public void upLoadImage(URL url, String filename, String path, String edittoken) throws IOException, SAXException {
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
String lineEnd = "rn";
String twoHyphens = "--";
String boundary = "*****";
try {
// ------------------ CLIENT REQUEST
FileInputStream fileInputStream = new FileInputStream(new File(path));
System.out.println("Filestreaminput");
EdittokenParser m = new EdittokenParser();
cookies = m.Returncookie();
con.setRequestProperty("Cookie", cookies);
// con.setRequestProperty("Content-Transfer-Encoding", "base64");
con.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
//con.setRequestProperty("Connection", "Keep-Alive");
//con.setRequestProperty("Content-Type",
// "multipart/form-data;boundary=" + boundary);
DataOutputStream dos = new DataOutputStream( con.getOutputStream() );
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name="wpUploadFile";"+"filename="/storage/emulated/0/Pictures/test.jpg"" + lineEnd);
dos.writeBytes("Content-Type: image/jpeg"+ lineEnd);
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 100000;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bytesAvailable];
// read file and write it into form...
int bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);
while (bytesRead > 0) {
dos.write(buffer, 0, bytesAvailable);
bytesAvailable = fileInputStream.available();
bytesAvailable = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name="wpDestFile";" + lineEnd);
dos.writeBytes(filename);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name="wpEditToken";" + lineEnd);
dos.writeBytes(edittoken);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name="wpUploadDescription";" + lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name="wpDestFileWarningAck";" + lineEnd);
dos.writeBytes("1");
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=title";" + lineEnd);
dos.writeBytes("Special:Upload");
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name="wpUpload";" + lineEnd);
dos.writeBytes("Upload Token");
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
System.out.println("Malformed url exception");
}
catch (IOException ioe) {
System.out.println("ioexception");
//cookies=cookies+ cookieList.get(0);
} finally {
con.disconnect();
}
The parameters I'm sending in are the URL (which in my case is mydomainname/mediawiki/index.php/Special:upload), the filename of what I want the uploaded file to be called on my MediaWiki site, the path of the file pointing to my image, and an edittoken that I URL encoded (do I need to do this?)
There are no errors, it just doesn't upload.
Thanks everyone!
You're help is greatly appreciated!
Oops sorry just an update the EditTokenParser just returns the cookies of the session.
I also tried using the MultipartEntity class but still no luck. This returns a nullpointer exception (but I don't know where!) This is the code I used:
public void uploadUserPhoto(File file, String url, String pageid, String edittoken) {
try {
EdittokenParser m = new EdittokenParser();
String cookies = m.Returncookie();
HttpPost httppost = new HttpPost(url);
MultipartEntity multipartEntity = new MultipartEntity();
System.out.println("File size:");
long length = file.length();
System.out.println(length);
System.out.println(file);
// Charset chars = Charset.forName("UTF-8");
FileBody fileBody = new FileBody(file, "test.jpg", "image/jpeg", "UTF-8");
//System.out.println(fileBody);
multipartEntity.addPart("wpUploadFile", fileBody);
multipartEntity.addPart("wpDestFile", new StringBody("Before"+pageid+".jpg"));
multipartEntity.addPart("wpUploadDescription", new StringBody(""));
multipartEntity.addPart("wpLicense", new StringBody(""));
System.out.println(edittoken);
multipartEntity.addPart("wpEditToken", new StringBody(edittoken));
multipartEntity.addPart("title", new StringBody("Special:Upload"));
// multipartEntity.addPart("wpDestFilWarningAck", new StringBody("1"));
multipartEntity.addPart("wpUpload", new StringBody("Upload file"));
httppost.addHeader("Cookie", cookies);
httppost.addHeader("Content-Type", "text/html; charset=utf-8");
httppost.setEntity(multipartEntity);
System.out.println("Does the post work here?");
mHttpClient.execute(httppost, new PhotoUploadResponseHandler());
System.out.println("Does the post not work here?");
} catch (Exception e) {
System.out.println(e);
}
}
private class PhotoUploadResponseHandler implements ResponseHandler {
@Override
public Object handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {
HttpEntity r_entity = response.getEntity();
String responseString = EntityUtils.toString(r_entity);
Log.d("UPLOAD", responseString);
return null;
}
}
And using Firebug for Firefox, this is what I need to send:
-----------------------------6910536459470485392043487056 Content-Disposition: form-data; name="wpUploadFile"; filename="DSC01886.JPG" Content-Type: image/jpeg (image bytes)-----------------------------6910536459470485392043487056 Content-Disposition: form-data; name="wpDestFile" testtesttest.jpg -----------------------------6910536459470485392043487056 Content-Disposition: form-data; name="wpUploadDescription" -----------------------------6910536459470485392043487056 Content-Disposition: form-data; name="wpLicense" -----------------------------6910536459470485392043487056 Content-Disposition: form-data; name="wpEditToken" 7207f4c1f1ea4f9608d51c5c7e04dccf+ -----------------------------6910536459470485392043487056 Content-Disposition: form-data; name="title" Special:Upload -----------------------------6910536459470485392043487056 Content-Disposition: form-data; name="wpDestFileWarningAck" 1 -----------------------------6910536459470485392043487056 Content-Disposition: form-data; name="wpUpload" Upload file -----------------------------6910536459470485392043487056--
链接地址: http://www.djcxy.com/p/48772.html