IndexController.java 1.77 KB
package com.bsth.controller;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
public class IndexController {

	Logger logger = LoggerFactory.getLogger(this.getClass());

	String indexSource;

	/**
	 * 构造函数
	 */
	public IndexController() {
		load();
	}

	/**
	 * 
	 * @Title: index 
	 * @Description: TODO(输出首页 index.html) 
	 */
	@RequestMapping
	public void index(String initFragment, HttpServletResponse resp) {

		// 初始打开的片段地址
		String outStr = StringUtils.replace(indexSource, "^_^initFragment^_^",
				initFragment == null ? "" : initFragment);

		resp.setContentType("text/html;charset=UTF-8");
		try {
			resp.getOutputStream().write(outStr.getBytes());
		} catch (IOException e) {
			logger.error("", e);
		}

	}

	@RequestMapping(value = "/index/load", method = RequestMethod.GET)
	public void load() {
		BufferedInputStream bis = null;

		try {
			InputStream is = IndexController.class.getClassLoader().getResourceAsStream("static/index.html");
			bis = new BufferedInputStream(is);
			StringBuilder source = new StringBuilder();
			byte[] buffer = new byte[bis.available()];

			while (bis.read(buffer) != -1) {
				source.append(new String(buffer));
			}

			indexSource = source.toString();
		} catch (Exception e) {
			logger.error("", e);
		} finally {
			try {
				bis.close();
			} catch (IOException e) {
				logger.error("", e);
			}
		}
	}
}