- //第一种方法
- String fileUri = request.getRequestURI();
- String contextPath = request.getContextPath();
- if (!contextPath.endsWith( "/ ")) {
- contextPath = contextPath + "/ ";
- }
- fileUri = fileUri.substring(contextPath.length(), fileUri.length());
- BufferedReader in = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(fileUri)));
- PrintWriter out = response.getWriter();
- response.setContentType( "*; charset=GBK ");
- String line = null;
- while ((line = in.readLine()) != null) {
- out.println(line);
- }
- in.close();
- out.close();
- //第二种方法
- URL resource = ActionForward.class.getResource("/vm/allpage/404.ftl");
- if(resource != null){
- URLConnection urlConnection;
- try {
- urlConnection = resource.openConnection();
- byte bytes[] = null;
- java.io.InputStream is = urlConnection.getInputStream();
- bytes = IOUtils.toByteArray(is);
- IOUtils.closeQuietly(is);
- IOUtils.write(bytes, System.out);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/091220137866.html
来源: http://www.codesnippet.cn/detail/091220137866.html