NutzCN Logo
问答 Nutz框架使用io流响应页面
发布于 2290天前 作者 locois 1511 次浏览 复制 上一个帖子 下一个帖子
标签:

需求:通过url访问WEB-INF下的静态.html文件下面是我的代码

@At("/html/?")
	@Ok("re")
	@Encoding(input="UTF-8",output="UTF-8")
    public String LoadHtml(int Id,ServletContext context, HttpServletResponse resp) {
		return "->:/WEB-INF/html/1.html";
    }

代码可行 可以实现访问;但是stateCode 是304 技术负责人认为内部重定向影响性能要我用io流的方式响应下面是我的思路

@At("/html/?")
	@Ok("re")
	@Encoding(input="UTF-8",output="UTF-8")
    public String LoadHtml(int Id,ServletContext context, HttpServletResponse resp) {
		InputStream in = null;
		try {
			in = new BufferedInputStream(context.getResourceAsStream("/WEB-INF/html/1.html"));
			byte[] buffer = new byte[10240];
			int leng = 0;
			while((leng = in.read(buffer)) != -1){
				String str = new String(buffer,0,leng);
				System.out.println(str);
				resp.getWriter().write(str);
			};
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return "";
    }

然后就不会了 请Wendal 指点迷津

7 回复

那是forward,不是304

如果自行写流 加@Ok("void")

是304呀 我抓包看到的

那不可能呢, "->" 是内部重定向, 浏览器不会感知的

那 是"->"性能高 还是自己写流高

哦, 看错了, 你说的是304呢!!! 304不是302哦

我找到方法了 用request.getRequestDispatcher("/WEB-INF/html/"+Id+".html").forward(request, resp); 就可以了 状态码200

304是好事啊

添加回复
请先登陆
回到顶部