首页 > 其他分享 >response下载

response下载

时间:2023-01-11 20:56:06浏览次数:46  
标签:realPath read resp guo byte response 下载 out

先通过FileInPutSteam的read(byte[])方法(单纯的read()方法是一次写入一个字节,返回值为asscm值,参数为字节数组的read方法返回值尾数组长度)写入数组,再通过resp的getOutPutStream创建一个文件输出流,通过write(byte[])方法将字节数组中的数据送出去

 

 

 

 

public class FileServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String realPath ="D:\\OLD\\77777\\Camera\\video_20210605_191810.mp4";
        System.out.println("下载文件的路径为:"+realPath);
        String filename = realPath.substring(realPath.lastIndexOf("//") + 1);
        resp.setHeader("Content-Disposition","attachment;filename="+filename);
        FileInputStream in=new FileInputStream(realPath);
        ServletOutputStream out=resp.getOutputStream();
        int len=0;
        byte[] guo=new byte[1024];
        while (in.read(guo)>-1){
            out.write(guo,0,guo.length);
        }
        in.close();
        out.close();


    }

 

标签:realPath,read,resp,guo,byte,response,下载,out
From: https://www.cnblogs.com/guojianglong/p/17044872.html

相关文章