如何从Java服务器套接字发送视频文件到浏览器下载?

huwehgph  于 2023-01-07  发布在  Java
关注(0)|答案(1)|浏览(210)

我正在尝试使用Java实现一个服务器,当我尝试发送一个文件时,比如视频或pdf,连接总是被重置,我目前使用的是OpenJDK 11和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();
}
ifsvaxew

ifsvaxew1#

application/force-download不是有效的媒体类型(请参阅Utility of HTTP header "Content-Type: application/force-download" for mobile?)。
您可以使用application/octet-stream来代替,但更好的选择是发送文件类型的正确媒体类型,即PDF文件的application/pdf等(参见How to get a file's Media Type (MIME type)?),然后发送单独的Content-Disposition: attachment头来触发下载。
此外,在创建Content-Length头时,使用String.valueOf()String.format()size整数转换为String比让operator+隐式地进行转换更有效(请参阅Concatenate integers to a String in Java了解原因)。
试试这个:

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/octet-stream\r\n";
    responseStr += "Content-Length: " + String.valueOf(size) + "\r\n";
    responseStr += "Content-Disposition: attachment; filename=\"" + file.getName() + "\"\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();
}

相关问题