二 整合web开发

一 整合Servlet

1 通过注解扫描完成 Servlet 组件的注册

1.编写servlet

@WebServlet(name="FirstServlet",urlPatterns="/first")
public class FirstServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
    // TODO Auto-generated method stub
    super.doGet(req, resp);
}
}

2.编写启动类,@ServletComponentScan需要开启

@SpringBootApplication
@ServletComponentScan //在 springBoot 启动时会扫描@WebServlet, 并将该类实例
化 pu
blic class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}

2通过方法完成 Servlet 组件的注册

通过ServletRegistrationBean实现

@Bean
public ServletRegistrationBean getServletRegistrationBean(){
    ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
    bean.addUrlMappings("/second");
    return bean;
}

二 整合Listener

1.通过注解

@WebListener
public class FirstListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
} @
Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("Listener...init......");
}
}

也需要配合@ServletComponentScan

2.通过注册

这次是ServletListenerRegistrationBean

/
**
* 注册 listener
*/
@Bean
public ServletListenerRegistrationBean getServletListenerRegistrationBean(){
    ServletListenerRegistrationBean bean= new 
                                    ServletListenerRegistrationBean(new SecondListener());
    return bean;
}

三 整合Filter

1.通过注解

@WebFilter(filterName="FirstFilter",urlPatterns="/first")
public class FirstFilter implements Filter {
  ...

2.通过注册

@Bean
public FilterRegistrationBean getFilterRegistrationBean(){
    FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter());
    //bean.addUrlPatterns(new String[]{"*.do","*.jsp"});
    bean.addUrlPatterns("/second");
    return bean;
}

四 访问静态资源

  1. Spring Boot的默认静态资源的路径为:
    spring.resources.static-locations=
    classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
    优先级从从高到低.
  2. 也可以是webapp。(war包情况下)
    具体参见:https://blog.csdn.net/plei_yue/article/details/79360645

五 文件上传和下载

spring.servlet.multipart.max-file-size限制单个文件的最大值
spring.servlet.multipart.max-request-size限制上传的多个文件的总大小

单个文件上传

    public String upload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return "上传失败";
        }
    ...
    }

多个文件上传

public String multiUpload(HttpServletRequest request) {
    List files = ((MultipartHttpServletRequest) request).getFiles("file");
    ...
}

文件下载

 //实现Spring Boot 的文件下载功能,映射网址为/download
   @RequestMapping("/download")
    public String downloadFile(HttpServletRequest request,
                               HttpServletResponse response) throws UnsupportedEncodingException {

        // 获取指定目录下的第一个文件
        File soureceFile = new File("E://a.txt");
        String fileName = soureceFile.getName(); //下载的文件名



        // 如果文件名存在,则进行下载
        if (soureceFile.exists()) {

            // 配置文件下载
            response.setHeader("content-type", "application/octet-stream");
            response.setContentType("application/octet-stream");
            // 下载文件能正常显示中文
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));

            // 实现文件下载
            byte[] buffer = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(soureceFile);
                bis = new BufferedInputStream(fis);
                OutputStream os = response.getOutputStream();
                int i = bis.read(buffer);
                while (i != -1) {
                    os.write(buffer, 0, i);
                    i = bis.read(buffer);
                }
                System.out.println("下载完成");
            }
            catch (Exception e) {
                System.out.println("下载出现异常");
            }
            finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return "end";
    }

你可能感兴趣的:(二 整合web开发)