我如何使用jax-rs发送文件

Thi*_*niz 12 java jax-rs download java-ee

如何使用jax-rs发送文件进行下载?

wod*_*dle 24

import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

public Response getFile(String contentType) {

        File f = new File("/tmp/file.doc");

        ResponseBuilder response = Response.ok(f);
        response.type(contentType);
        response.header("Content-Disposition", "attachment; filename=\"file.doc\"");
        return response.build();
    }
Run Code Online (Sandbox Code Playgroud)

  • 将f转换为Object for Response.ok()真的有必要吗?无论如何,文件继承了Object. (4认同)