我正在制作一个Java程序,该程序可以使用其api将(主要是mp4)文件上传到托管者openload.co,您可以在其中找到here。
我的程序可以上传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;
}
最佳答案
我发现出了什么问题。我比较了testvideo本身和上传的videovideo的实际大小。上传的文件的大小比原始文件大25字节。事实证明,无论出于何种原因,都将这一行添加到videofilebytes中:
dfileOutput.writeBytes("Content-Type: video/mp4"+crlf);
现在,我已将其删除,一切正常。