全部文档
当前文档

暂无内容

如果没有找到您期望的内容,请尝试其他搜索词

文档中心

表单上传(Android)

最近更新时间:2023-01-12 11:52:17

Post Object

以下代码为表单上传Object示例代码:

/***
*  下面是以数据流的形式上传,代码较直观,当然可以另行实现
**/
public void postObject() {

    final File file = new File("<filePath>");
    String key = "tttt";
    Map<String,String> postData = new HashMap<String,String>();
    postData.put("acl","public-read");
    postData.put("key", key);
    List<String> unknowValueField = new ArrayList<String>();
    unknowValueField.add("name");
  
    // 计算policy、signature  下面会用到
    PostObjectFormFields  fields =  client.getObjectFormFields("<bucketName>",file.getName(),postData,unknowValueField);

    System.out.println("*********** signature:  " + signature);
    String uploadUrl = "http://" + "<endPoint>" + "/" + "<bucketName>";
    String end = "\r\n";
    String twoHyphens = "--";
    String boundary = "******";
    try {
        URL url = new URL(uploadUrl);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url
                .openConnection();
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setUseCaches(false);
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
        httpURLConnection.setRequestProperty("Charset", "UTF-8");
        httpURLConnection.setRequestProperty("Content-Type",
                "multipart/form-data;boundary=" + boundary);

        DataOutputStream dos = new DataOutputStream(httpURLConnection.getOutputStream());

        // 写入key
        dos.writeBytes(twoHyphens + boundary + end);
        dos.writeBytes("Content-Disposition: form-data; name=\"key\"" + end);
        dos.writeBytes(end);
        dos.writeBytes(key);
        dos.writeBytes(end);

        // 写入kssAccessId
        dos.writeBytes(twoHyphens + boundary + end);
        dos.writeBytes("Content-Disposition: form-data; name=\"KSSAccessKeyId\"" + end);
        dos.writeBytes(end);
        dos.writeBytes(fields.getKssAccessKeyId());
        dos.writeBytes(end);

        // 写入policy
        dos.writeBytes(twoHyphens + boundary + end);
        dos.writeBytes("Content-Disposition: form-data; name=\"Policy\"" + end);
        dos.writeBytes(end);
        dos.writeBytes(fields.getPolicy());
        dos.writeBytes(end);

        // 写入signature
        dos.writeBytes(twoHyphens + boundary + end);
        dos.writeBytes("Content-Disposition: form-data; name=\"Signature\"" + end);
        dos.writeBytes(end);
        dos.writeBytes(fields.getSignature());
        dos.writeBytes(end);

        dos.writeBytes(twoHyphens + boundary + end);
        dos.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\""
                + file.getName()
                + "\"" + end);
        dos.writeBytes(end);

        //将SD 文件通过输入流读到Java代码中
        FileInputStream fis = new FileInputStream(file);
        byte[] buffer = new byte[8192]; // 8k
        int count = 0;
        while ((count = fis.read(buffer)) != -1) {
            dos.write(buffer, 0, count);
        }
        fis.close();
        System.out.println("file send to server............");
        dos.writeBytes(end);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
        dos.flush();

        //读取服务器返回结果
        int code = httpURLConnection.getResponseCode();
        System.out.println("response code: " + code);
        InputStream is = null;
        if (code == 200) {
            is = httpURLConnection.getInputStream();
        } else {
            is = httpURLConnection.getErrorStream();
        }
        InputStreamReader isr = new InputStreamReader(is, "utf-8");
        BufferedReader br = new BufferedReader(isr);

        StringBuilder sb = new StringBuilder();
        String result;

        while ((result = br.readLine()) != null) {
            sb.append(result);
        }
        System.out.println(sb.toString());
        is.close();
        dos.close();

    } catch (Exception e) {
        e.printStackTrace();
        System.out.println(e.getMessage());
    }
}

注:

  • 如果bucket空间权限为公开读写,那么只需要传keyfile两个参数,且两个参数的顺序不能乱。

  • 如果bucket空间权限为私密,这个时候需要传以下字段,详见文档说明

名称 说明 是否必须
key objectKey
KSSAccessKeyId ak,此处必须大写,其他的键名大小写没有要求
policy 安全策略,经过Base64之后的值,可通过PostObjectFormFields实例获取
signature 根据sk和policy计算的签名信息,可通过PostObjectFormFields实例获取
file 文件或文本内容

这些字段的顺序必须按照key首位、file尾部,中间的几个随意的规则,否则会报错AccessDenied。另外,如果想查看示例源码,请点击查看

文档导读
纯净模式常规模式

纯净模式

点击可全屏预览文档内容
文档反馈