I am trying to implement a server using Java. When I try to send a file, such as video or pdf, the connection is always reset. I am currently using OpenJDK 11 and Ubuntu.
void binaryResponse(OutputStream clientOutput, File file) throws IOException {
int size= (int) file.length();
String responseStr="HTTP/1.1 200 OK\r\n";
responseStr+="Content-Type: application/force-download\r\n";
responseStr+="Content-Length: " + size + "\r\n\r\n";
clientOutput.write(responseStr.getBytes());
FileInputStream fis = new FileInputStream(file);
int bytes;
byte[] buffer = new byte[4*1024];
while (size > 0 && (bytes = fis.read(buffer, 0, Math.min(buffer.length, size))) != -1){
clientOutput.write(buffer, 0, bytes);
size -= bytes;
}
clientOutput.flush();
fis.close();
}