Reputation: 21
I'm making a java-program which is able to upload a (mainly mp4) file to the hoster openload.co using their api, which u can find here.
My program is able to upload a mp4 video, but it isnt playable. When i download a previously uploaded video and check its details via the properties of the file, the video and audio information are lacking, so it seems as if the server doesnt know what to do with the file-bytes, although it recognizes the content-type, size and filename.
I've checked google and this forum, but couldnt find any answer yet.
Here's the code that takes care of the upload procedure. I hope someone knows what is missing.
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;
}
Upvotes: 1
Views: 848
Reputation: 21
I've found out whats wrong. I compared the actual size of the testvideo itself and of the uploaded one. The size of the uploaded one was 25 Bytes bigger than the original. It turned out that this line was added onto the videofilebytes, for whatever reason:
dfileOutput.writeBytes("Content-Type: video/mp4"+crlf);
Now that i've deleted it everything works fine.
Upvotes: 1