读取项目包中的文件,读取项目包中文件,//第一种方法Strin
分享于 点击 5138 次 点评:20
读取项目包中的文件,读取项目包中文件,//第一种方法Strin
//第一种方法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://byrx.net
用户点评