Write an InputStream to an HttpServletResponse

前端 未结 3 1448
一个人的身影
一个人的身影 2020-12-03 07:02

I have an InputStream that I want written to a HttpServletResponse. There\'s this approach, which takes too long due to the use of byte[]

InputStream is = ge         


        
3条回答
  •  清歌不尽
    2020-12-03 07:49

    BufferedInputStream in = null;
    BufferedOutputStream out = null;
    OutputStream os;
    os = new BufferedOutputStream(response.getOutputStream());
    in = new BufferedInputStream(new FileInputStream(file));
    out = new BufferedOutputStream(os);
    byte[] buffer = new byte[1024 * 8];
    int j = -1;
    while ((j = in.read(buffer)) != -1) {
        out.write(buffer, 0, j);
    }
    

提交回复
热议问题