response下载

先通过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();


    }

 

posted on 2023-01-11 20:45  大风吹过12138  阅读(58)  评论(0)    收藏  举报

导航