Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java jax-rs从服务器下载文件_Java_Jax Rs - Fatal编程技术网

Java jax-rs从服务器下载文件

Java jax-rs从服务器下载文件,java,jax-rs,Java,Jax Rs,我正试图从rest请求中从服务器下载文件。请求已发送,似乎一切正常,但我得到: org.jboss.resteasy.spi.writereException:java.io.IOException:流已关闭。 我认为这可能是因为JSF正在事后处理响应。通常我使用:facesContext.responseComplete() 但是对于REST请求,我无法访问facesContext(它是空的) 以下是我的java代码: response.setContentType(pieceJointe.g

我正试图从rest请求中从服务器下载文件。请求已发送,似乎一切正常,但我得到:
org.jboss.resteasy.spi.writereException:java.io.IOException:流已关闭。

我认为这可能是因为JSF正在事后处理响应。通常我使用:
facesContext.responseComplete()
但是对于REST请求,我无法访问facesContext(它是空的)

以下是我的java代码:

response.setContentType(pieceJointe.getContentType());
response.addHeader("Content-disposition", "attachment; filename=\"" + pieceJointe.getNom() +"\"");
        if (file.exists()) {
            FileInputStream fi;
            try {
                fi = new FileInputStream(file);
                ServletOutputStream os = response.getOutputStream();

                int b = fi.read();

                while (b != -1) {
                    os.write(b);
                    b = fi.read();
                }

                os.flush();
                os.close();
                fi.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }                   
        }
我做错了什么


谢谢

尽量不要关闭响应输出流。您通常不需要这样做,因为不是您打开的。好的,谢谢,这解决了关闭流的问题。但该文件仍然没有下载。什么也没发生:尝试将内容类型“应用程序/八位字节流”设置为相同,什么也没发生。这与回应有关吗?因为它是一个rest请求,所以我通过以下方式获得响应:
publicstringdownloadRightatif(@PathParam(“id”)Integer-id,@Context-HttpServletResponse,@Context-HttpServletRequest-req){…}
对于JAX-RS,可能需要检查此链接。有很多例子