如何从android上传图片到MediaWiki网站

我试图从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 = "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();
    }

我发送的参数是URL(在我的情况下是mydomainname / mediawiki / index.php / Special:upload),我希望在我的MediaWiki站点上调用上传文件的文件名,指向我的图像的文件,以及我编码的edittoken(我是否需要这样做?)

没有错误,它只是不上传。

感谢大家!

非常感谢您的帮助!

糟糕对不起,只是一个更新EditTokenParser只是返回会话的cookie。

我也尝试使用MultipartEntity类,但仍然没有运气。 这将返回一个空指针异常(但我不知道在哪里!)这是我使用的代码:

 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”Content-Type:image / jpeg(image bytes)----------------------------- 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--

链接地址: http://www.djcxy.com/p/48771.html

上一篇: How to upload image to MediaWiki site from android

下一篇: is nhibernate 3.0 ready for production