开发者社区> 问答> 正文

使用API怎么上传文件?

官网demo上传中testBucket, uploadObject, uploadFilePath这三个参数应该怎么填???求大神回答
// 上传

    Button upload = (Button) findViewById(R.id.upload);
    upload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    new PutObjectSamples(oss, testBucket, uploadObject, uploadFilePath).asyncPutObjectFromLocalFile();
                }
            }).start();
        }
    });

展开
收起
helloworld0208 2017-05-17 16:16:23 2693 0
1 条回答
写回答
取消 提交回答
  • 创建工具类 public class WinkUtils { public static ClientConfig getClientConfig(){ ClientConfig config=new ClientConfig(); SSLContext sc; try { sc=SSLContext.getInstance("SSL"); sc.init(null, getTrustManager(), new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); config.setBypassHostnameVerification(true); config.connectTimeout(100000); config.readTimeout(100000); config.followRedirects(false); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyManagementException e) { e.printStackTrace(); } return config; } public static TrustManager[] getTrustManager(){ TrustManager[] trustAllCerts=new TrustManager[]{ new X509TrustManager(){ public void checkClientTrusted(X509Certificate[] arg0,String arg1) throws CertificateException {} public void checkServerTrusted(X509Certificate[] arg0,String arg1) throws CertificateException {} public X509Certificate[] getAcceptedIssuers() { return null; } } }; return trustAllCerts; } } 第三步:写入图片数据到Bmob上 /** * 功能:根据传入的url路径插入图片数据 * @param url 上传图片的url路径 * @param data 传入到Bmob中的图片二进制数据 * @return 插入成功返回的json格式字符串 */ public static String setInsertGoodsData(String url,byte[] data){ String result=null; RestClient restClient=new RestClient(WinkUtils.getClientConfig()); Resource resource=restClient.resource(url); resource.header("X-Bmob-Application-Id", "你自己的APPID"); resource.header("X-Bmob-REST-API-Key",“你自己的APPKEY”); resource.header("Content-Type", "image/jpeg"); ClientResponse response=resource.post(data); int code=response.getStatusCode(); System.out.println("结果码:"+code); if(code==201){ result=response.getEntity(String.class); } return result; } 根据返回的result当中就包含图片上传之后在Bmob上的路径。操作完毕。

    答案来源于网络

    2019-09-25 21:01:43
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Boot2.0实战Redis分布式缓存 立即下载
CUDA MATH API 立即下载
API PLAYBOOK 立即下载