zoukankan      html  css  js  c++  java
  • sae 上传文件 java实现

    不讲废话上代码

     1 /**
     2      * 上传语音消息至sae服务器
     3      * 
     4      * @param requestUrl 上传链接    
     5      * @param requestMethod http请求方式
     6      */
     7     
     8     public static boolean httpRequestUploadVoice(String requestUrl, String requestMethod, String fileName){
     9         int byteread = 0;
    10         boolean isUpload = false;
    11         try {
    12             URL url = new URL(requestUrl);
    13             HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
    14 
    15             httpUrlConn.setDoOutput(true);
    16             httpUrlConn.setDoInput(true);
    17             httpUrlConn.setUseCaches(false);
    18             // 设置请求方式(GET/POST)
    19             httpUrlConn.setRequestMethod(requestMethod);
    20 
    21             if ("GET".equalsIgnoreCase(requestMethod))
    22                 httpUrlConn.connect();
    23 
    24             
    25 
    26             
    27             InputStream inStream = httpUrlConn.getInputStream();
    28             ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    29             byte[] buffer = new byte[1204];
    30             int length;
    31             while ((byteread = inStream.read(buffer)) != -1) {
    32                 outputStream.write(buffer, 0, byteread);
    33             }
    34             //sae 不支持追加 所以就只能放到一个  字节数组里
    35             byte[] res = outputStream.toByteArray();
    36             //上传到 sae
    37             SaeStorage st = new SaeStorage();
    38             //三个参数   domain - 存储域名      fileName - 文件名    content - 文件内容
    39             isUpload = st.write("test",fileName, res);
    40             
    41             inStream.close();
    42             inStream = null;
    43             httpUrlConn.disconnect();
    44         
    45             } catch (FileNotFoundException e) {
    46                 e.printStackTrace();
    47             } catch (IOException e) {
    48                 e.printStackTrace();
    49             }
    50         
    51             return isUpload;
    52             
    53     }
  • 相关阅读:
    监控里的主码流和子码流是什么意思
    监控硬盘容量计算
    一个能让你了解所有函数调用顺序的Android库
    电工选线
    oracle linux dtrace
    list all of the Oracle 12c hidden undocumented parameters
    Oracle Extended Tracing
    window 驱动开发
    win7 x64 dtrace
    How to Use Dtrace Tracing Ruby Executing
  • 原文地址:https://www.cnblogs.com/MDK-L/p/3782118.html
Copyright © 2011-2022 走看看