2016-10-19 14 views
0

私はhereというapiを使って(主にmp4)ファイルをホストloadload.coにアップロードできるJavaプログラムを作っています。via java uploaded mp4ファイルは再生できません/壊れています

私のプログラムはmp4ビデオをアップロードできますが、再生できません。以前にアップロードしたビデオをダウンロードしてその詳細をファイルのプロパティで確認すると、ビデオとオーディオの情報が不足しているため、サーバーがコンテンツバイトをどのように扱うかわからないように見えますが、タイプ、サイズ、およびファイル名。

Googleとこのフォーラムをチェックしましたが、まだ回答が見つかりませんでした。

アップロード手順を担当するコードは次のとおりです。誰かが逃していることを誰かが知っていれば幸いです

private static String uploadFile(URL uploadURL, Path file, String fileName, String fileNameWithType) throws IOException{ 
    /*the size of the file, which is to be uploaded */ 
    long s = Files.size(file); 
    /*the amount of additional bytes sent with the file */ 
    long offset = 220; 
    /*the sum of the filesize and additional bytes */ 
    long actualBodySize = offset + s; 

    // returned server response 
    String response = null; 
    //HTTP line-break 
    String crlf = "\r\n"; 
    //mainly needed for end of multipart/form-data request 
    String twoHyphens = "--"; 
    //the fields are separated by this boundary, a random number 
    String boundary = "---------------------"+Long.toString(System.currentTimeMillis()); 

//--------------------------------------------------------------------------------------------------------  
    HttpsURLConnection OutputConnection = (HttpsURLConnection) uploadURL.openConnection(); 
    OutputConnection.setDoOutput(true); 
//--------------------------------------------------------------------------------------------------------   
    OutputConnection.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary); 
    //allows java to start sending data via network immediatly 
    OutputConnection.setFixedLengthStreamingMode(actualBodySize); 
    OutputConnection.setRequestProperty("Connection", "Keep-Alive"); 
    OutputConnection.setRequestProperty("Cache-Control", "no-cache"); 
//--------------------------------------------------------------------------------------------------------                       
//setting up the I/O-Streams 

//OutputConnection.connect(); //not needed, getOutputStream does connect() on its own 
    try(InputStream fileInput = newInputStream(file, READ); 
     BufferedInputStream bfileInput = new BufferedInputStream(fileInput); 
     OutputStream fileOutput = OutputConnection.getOutputStream(); 
     BufferedOutputStream bfileOutput = new BufferedOutputStream(fileOutput); 
     DataOutputStream dfileOutput = new DataOutputStream(bfileOutput)){ 
//--------------------------------------------------------------------------------------------------------- 
//  manually writing the multipart/form-data request 
     dfileOutput.writeBytes(twoHyphens + boundary + crlf); 
     dfileOutput.writeBytes("Content-Disposition: form-data; name=\"" + 
     fileName + "\"; filename=\"" + 
     fileNameWithType + "\"" + crlf); 
     dfileOutput.writeBytes(crlf); 
     dfileOutput.writeBytes("Content-Type: video/mp4"+crlf); 
//  dfileOutput.writeBytes("Content-Transfer-Encoding: binary" + crlf + crlf); 
//--------------------------------------------------------------------------------------------------------------- 
//  uploading the file 
     for(int byteCode = bfileInput.read(); byteCode >= 0; byteCode = bfileInput.read()){ 
      dfileOutput.write(byteCode); 
     } 
//---------------------------------------------------------------------------------------------------------------    
//  manually writing the end-part of the multipart/form-data request 
     dfileOutput.writeBytes(crlf); 
     dfileOutput.writeBytes(twoHyphens + boundary + twoHyphens + crlf); 
     dfileOutput.flush(); 
//---------------------------------------------------------------------------------------------------------------- 
//  setting up inputstream in order to get the server response 
     ... 
//  extracting the download-url of the json-server-response 
     ... 

    } 
    OutputConnection.disconnect(); 
    return response; 
} 
+0

あなたはBase64でコンテンツを転送していると主張していますが、そうではありません。 – Kayaman

+0

私の悪い、私はそれを削除することを忘れました。私はさまざまなことを見つけましたが、サーバーはcontent-transfer-encodingフィールドを無視しているようですが、これは問題ではありません。 – javaman

+0

**小サイズの**以前にアップロードしたテストビデオへのリンクを提供します。アイデアは、ファイルのバイト内容をチェックすることです(ヘッダーが欠落していますか?余分なバイトが壊れていますか?...など)。あなたがする必要がある場合、テストを数秒で行います...何が間違っているかを見る/確認できない場合、なぜ再生していないのか説明できません。 –

答えて

0

私は何が間違っているかを見つけました。私はテストビデオそのものとアップロードされたビデオの実際のサイズを比較しました。アップロードされたファイルのサイズは元のサイズよりも25バイト大きかった。これは、何らかの理由で、この行はvideofilebytesに追加されたことが判明:

dfileOutput.writeBytes("Content-Type: video/mp4"+crlf); 

今私はそれを削除したことすべてが正常に動作します。

関連する問題