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   大风吹过12138  阅读(50)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
点击右上角即可分享
微信分享提示