HTTP大文件分片(分流)上傳 -开发者知识库

HTTP大文件分片(分流)上傳 -开发者知识库,第1张

JAVA服務器端500MB文件如何分流為每200MB進行HTTP數據傳輸?

2 个解决方案

#1


如下代碼如何進行分流改造:


private String UploadVideo(Map<String, String> tmpMap,String tPathName) throws ClientProtocolException, IOException {
        // 返回tETag
        String tETag = "";
        // 連接
        HttpURLConnection conn = null;
        // 傳遞參數
        OutputStreamWriter tStreamWriter = null;
        // 接受參數
        InputStream tInputStream = null;

        String nextLine = "\r\n";
        String dividerStart = "--";
        String BOUNDARY = "******8888668ec";

        try{
            URL url = new URL(tmpMap.get("url"));
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("PUT");// 提交模式
            conn.setConnectTimeout(DI_CommonType.ConnectTimeout);// 連接超時 單位毫秒
            conn.setDoOutput(true);// 是否輸入參數
            conn.setDoInput(true); // 是否接收參數
            conn.setUseCaches(false);// 是否緩存
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Charset","UTF-8");
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
            // 分界符
            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary="   BOUNDARY);
            // 定義寫入數據流
            DataOutputStream tDataOutputStream = new DataOutputStream(conn.getOutputStream());
            tDataOutputStream.writeBytes(dividerStart   BOUNDARY   nextLine);
            //設置與上傳文件相關的信息
            tDataOutputStream.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\""
                      tFilePathc.substring(tFilePathc.lastIndexOf("/")   1)   "\""   nextLine);
            tDataOutputStream.writeBytes(nextLine);
            FileInputStream fis = new FileInputStream(tFilePathc);
            byte[] buffer = new byte[1024 * 32];
            int count;
            // 讀取文件內容,並寫入OutputStream對象
            while ((count = fis.read(buffer)) != -1) {
                tDataOutputStream.write(buffer, 0, count);
            }
            fis.close();
            tDataOutputStream.writeBytes(nextLine);
            tDataOutputStream.writeBytes(dividerStart   BOUNDARY   dividerStart   nextLine);
            tDataOutputStream.flush();

            // 接收參數
            int tResponseCode = conn.getResponseCode();
            Map<String,List<String>> headerFields = conn.getHeaderFields();
            System.out.println(headerFields.get("ETag"));

            if (200==tResponseCode) {
                tETag = headerFields.get("ETag").toString();
            }

        }catch(IOException e){
            try {
                System.out.println(conn.getResponseCode());
            } catch (IOException e1) {
                e1.printStackTrace();
            }
//            e.printStackTrace();
            System.out.println("通信異常! ");
        }finally {
            try {
                if (null!=tStreamWriter) {
                    tStreamWriter.close();
                }
                if (null!=tInputStream) {
                    tInputStream.close();
                }
                if (null!=conn) {
                    conn.disconnect();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        return tETag;
    }

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复