SpringBoot jar包部署,访问HTML页面

 方式一:

通过application.properties配置文件进行设置

项目启动后直接访问templates下的html

spring.resources.static-locations=classpath:/templates/
spring.freemarker.suffix=.html
spring.freemarker.template-loader-path=classpath:/templates/

方式二:

通过ClassPathResource获取resources下的HTML文件

通过OutputStream输出流将HTML文件写到浏览器

	@RequestMapping(value = "/test", method = RequestMethod.GET)
	public void test(HttpServletRequest request, HttpServletResponse response) throws IOException {

                //使用ClassPathResource获取resources下的HTML文件
		ClassPathResource resource = new ClassPathResource("temp.html");
                //获取文件的输入流
		InputStream in = resource.getInputStream();

		// 创建输出流
		OutputStream out = response.getOutputStream();
		// 缓存区
		byte buffer[] = new byte[1024];
		int len = 0;
		// 循环将输入流中的内容读取到缓冲区中
		while ((len = in.read(buffer)) > 0) {
			out.write(buffer, 0, len);
		}

		// 关闭
		in.close();
		out.close();
	}

方式三:

后台拼接HTML页面

通过response.getWriter().write()将HTML代码写到浏览器

	@RequestMapping(value = "/test1", method = RequestMethod.GET)
	public void test1(HttpServletRequest request, HttpServletResponse response) throws IOException {
		StringBuilder builder = new StringBuilder();
		
		  builder.append("");
		  builder.append("");
		  builder.append("test");
		  builder.append("");
		  builder.append("");

		  response.setHeader("content-type", "text/html;charset=UTF-8");
		  response.getWriter().write(builder.toString());
	}

 

你可能感兴趣的:(SpringBoot)