我正在尝试从Android手机上传图片到自定义MediaWiki网站。不幸的是,我试图上传的图片不起作用。这是我用来发送多部分请求的功能:
public void upLoadImage(URL url, String filename, String path, String edittoken) throws IOException, SAXException {
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
String lineEnd = "\r\n";
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();
}
我发送的参数是URL(在我的例子中是mydomainname / mediawiki / index.php / Special:upload),我希望在MediaWiki网站上调用上传文件的文件名,指向我的图像的文件的路径,以及我编码URL的edittoken(我需要这样做吗?)
没有错误,只是没有上传。
谢谢大家!
非常感谢您的帮助!
对不起,只是更新EditTokenParser只返回会话的cookie。
我也尝试过使用MultipartEntity类,但仍然没有运气。这会返回一个nullpointer异常(但我不知道在哪里!)这是我使用的代码:
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;
}
}
使用Firebug for Firefox,这是我需要发送的内容:
----------------------------- 6910536459470485392043487056 Content-Disposition:form-data; NAME = “wpUploadFile”; filename =“DSC01886.JPG”内容类型:image / jpeg(图像字节)----------------------------- 6910536459470485392043487056 Content-处置:表格数据; 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”上传文件----------------------------- 6910536459470485392043487056 -