gpt4 book ai didi

java - 通过java上传mp4文件无法播放/已损坏

转载 作者:行者123 更新时间:2023-12-01 09:21:34 25 4
gpt4 key购买 nike

我正在制作一个java程序,它能够使用他们的api将(主要是mp4)文件上传到主机openload.co,你可以找到here .

我的程序可以上传 mp4 视频,但无法播放。当我下载以前上传的视频并通过文件的属性检查其详细信息时,缺少视频和音频信息,因此服务器似乎不知道如何处理文件字节,尽管它识别了内容 -类型、大小和文件名。

我检查了谷歌和这个论坛,但还没有找到任何答案。

这是负责上传过程的代码。我希望有人知道缺少什么。

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;
}

最佳答案

我发现出了什么问题。我比较了测试视频本身和上传视频的实际大小。上传的大小比原始大小大 25 字节。事实证明,无论出于何种原因,这一行都被添加到了视频文件字节中:

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

现在我已经删除了它,一切正常。

关于java - 通过java上传mp4文件无法播放/已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40137967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com