天天看點

微信公衆平台開發視訊上傳

    public String upVideo(String filePath, String accessToken, String title, String introduction) throws IOException {

        File file = new File(filePath);

        JSONObject j = new JSONObject();

        j.put("title", title);

        j.put("introduction", introduction);

        String action = "http://api.weixin.qq.com/cgi-bin/material/add_material?access_token=" + accessToken;

        URL urlFile = new URL(action);

        String result = null;

        if (!file.exists() || !file.isFile()) {

            throw new IOException("上傳的視訊不存在");

        }

        long filelength = file.length();  

        String fileName=file.getName();  

//     String suffix=fileName.substring(fileName.lastIndexOf("."),fileName.length());  

        String type="video/mp4"; //我這裡寫死  

        HttpURLConnection con = (HttpURLConnection) urlFile.openConnection();

        con.setRequestMethod("POST");// 以Post方式送出表單,預設get方式

        con.setDoInput(true);

        con.setDoOutput(true);

        con.setUseCaches(false);// post方式不能使用緩存

        // 設定請求頭資訊

        con.setRequestProperty("Connection", "Keep-Alive");

        con.setRequestProperty("Charset", "UTF-8");

        // 設定邊界,這裡的boundary是http協定裡面的分割符,不懂的可惜百度(http 協定 boundary),這裡boundary 可以是任意的值(111,2222)都行  

        String BOUNDARY = "----------" + System.currentTimeMillis();

        con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);

        // 請求正文資訊

        // 第一部分:

        StringBuilder sb = new StringBuilder();

        //這塊是post送出type的值也就是檔案對應的mime類型值 

        sb.append("--"); // 必須多兩道線這裡說明下,這兩個橫杠是http協定要求的,用來分隔送出的參數用的,不懂的可以看看http 協定頭  

        sb.append(BOUNDARY);

        sb.append("\r\n");

        sb.append("Content-Disposition: form-data;name=\"type\" \r\n\r\n");//這裡是參數名,參數名和值之間要用兩次  

        sb.append(type+"\r\n"); //參數的值

        //這塊是上傳video是必須的參數,你們可以在這裡根據檔案類型做if/else 判斷  

        sb.append("--"); // 必須多兩道線  

        sb.append(BOUNDARY);  

        sb.append("\r\n");  

        sb.append("Content-Disposition: form-data;name=\"description\" \r\n\r\n");  

        sb.append(j.toString()+"\r\n");  

        sb.append("--"); // 必須多兩道線  

        sb.append(BOUNDARY);  

        sb.append("\r\n");  

        //這裡是media參數相關的資訊,這裡是否能分開下我沒有試,感興趣的可以試試  

        sb.append("Content-Disposition: form-data;name=\"media\";filename=\""  

                + fileName + "\";filelength=\"" + filelength + "\" \r\n");  

        sb.append("Content-Type:application/octet-stream\r\n\r\n");  

        System.out.println(sb.toString());  

        byte[] head = sb.toString().getBytes("utf-8");

        // 獲得輸出流

        OutputStream out = new DataOutputStream(con.getOutputStream());

        // 輸出表頭

        out.write(head);

        // 檔案正文部分

        // 把檔案以流檔案的方式推入到url中

        DataInputStream in = new DataInputStream(new FileInputStream(file));

        int bytes = 0;

        byte[] bufferOut = new byte[1024];

        while ((bytes = in.read(bufferOut)) != -1) {

            out.write(bufferOut, 0, bytes);

        }

        in.close();

        // 結尾部分

        byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");// 定義最後資料分隔線

        out.write(foot);

        out.flush();

        out.close();

        StringBuffer buffer = new StringBuffer();

        BufferedReader reader = null;

        try {

            // 定義BufferedReader輸入流來讀取URL的響應

            reader = new BufferedReader(new InputStreamReader(con.getInputStream()));

            String line = null;

            while ((line = reader.readLine()) != null) {

                System.out.println("line------------------------------>"+line);

                buffer.append(line);

            }

            if (result == null)

                result = buffer.toString();

        } catch (IOException e) {

            System.out.println("發送POST請求出現異常!" + e);

            e.printStackTrace();

            throw new IOException("資料讀取異常");

        } finally {

            if (reader != null) {

                reader.close();

            }

        }

        return result;

    }

繼續閱讀