会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132495个问题
Python 全系列/第十阶段:Flask百战电商后台项目/Flask百战电商后台项目 28696楼

canvas.png

<servlet>
    <servlet-name>autoStartServlet</servlet-name>
    <servlet-class>com.bjsxt.servlet.AutoStartServlet</servlet-class>
    <init-param>
        <param-name>path</param-name>
        <param-value>image</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>autoStartServlet</servlet-name>
    <url-pattern>/auto.do</url-pattern>
</servlet-mapping>
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

public class AutoStartServlet extends HttpServlet {
    @Override
    public void init(ServletConfig config) throws ServletException {
        ServletConfig servletConfig = this.getServletConfig();
        String value = servletConfig.getInitParameter("path");
        ServletContext servletContext = this.getServletContext();
        servletContext.setAttribute("path",value);

    }
}


package com.bjsxt.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

public class DemoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        String path = (String) servletContext.getAttribute("path");
        String realPath = servletContext.getRealPath(path+"/zhw.jpg");
        File file =new File(realPath);
        FileInputStream fis=new FileInputStream(file);
        byte[] buff=new byte[fis.available()];
        fis.read(buff);

        resp.addHeader("Context-disposition","attachment;filename="+new String(file.getName().getBytes("gbk"),"iso-8859-1"));
        OutputStream os=resp.getOutputStream();
        os.write(buff);
        os.flush();
        os.close();
    }
}

老师我的状态码报错五百。  demo目录下为什么是空啊!  我去了本地文件里看了,里面是有图片的。

JAVA 全系列/第五阶段:JavaWeb开发/Servlet技术详解(旧) 28697楼
JAVA 全系列/第二十三阶段:容器管理技术/Docker 28700楼
微服务/第一阶段:SSM 框架和项目开发/(旧)Mybatis 28703楼
JAVA 全系列/第六阶段:项目管理与SSM框架/Spring 28704楼
Python 全系列/第十五阶段:Python 爬虫开发/爬虫基础(旧) 28706楼
大数据全系列/第一阶段:Linux 操作系统/Linux操作系统概述与安装 28707楼
JAVA 全系列/第五阶段:JavaWeb开发/Servlet技术详解(旧) 28708楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637