当前位置: 首页 > news >正文

查看文件源代码功能实现

我们经常遇到,查看文件源代码的功能。Struts 自带的例子是这样的。
 
其中思路是这样的:将文件流转换为List 输出出来。
用到了
第一种:读取Class
InputStream in = getClass().getResourceAsStream(className);
第二种:一般文件
InputStream in = ClassLoaderUtil.getResourceAsStream(page.substring(indexOf+1), getClass()); 

//or 

    in = servletContext.getResourceAsStream(page);
 
第三种:配置文件
new URL(config).openStream()
 
 
/* 
* $Id: ViewSourceAction.java 570518 2007-08-28 18:26:48Z jholmes $ 

* Licensed to the Apache Software Foundation (ASF) under one 
* or more contributor license agreements.    See the NOTICE file 
* distributed with this work for additional information 
* regarding copyright ownership.    The ASF licenses this file 
* to you under the Apache License, Version 2.0 (the 
* "License"); you may not use this file except in compliance 
* with the License.    You may obtain a copy of the License at 

*    http://www.apache.org/licenses/LICENSE-2.0 

* Unless required by applicable law or agreed to in writing, 
* software distributed under the License is distributed on an 
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
* KIND, either express or implied.    See the License for the 
* specific language governing permissions and limitations 
* under the License. 
*/
 
package org.apache.struts2.showcase.source; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.List; 

import javax.servlet.ServletContext; 
import javax.servlet.http.HttpServletRequest; 

import org.apache.struts2.ServletActionContext; 
import org.apache.struts2.util.ServletContextAware; 

import com.opensymphony.xwork2.ActionSupport; 
import com.opensymphony.xwork2.util.ClassLoaderUtil; 

/** 
* Processes configuration, page, and action class paths to create snippets 
* of the files for display. 
*/
 
public  class ViewSourceAction  extends ActionSupport  implements ServletContextAware { 

         private String page; 
         private String className; 
         private String config; 

         /** 
         * 数据行列表 
         */
 
         private List pageLines; 
         private List classLines; 
         private List configLines; 

         private  int configLine; 
         private  int padding = 10; 

         private ServletContext servletContext; 

         public String execute()  throws MalformedURLException, IOException { 

                 if (page !=  null && page.trim().length() > 0) { 

                         int indexOf = page.indexOf( "//"); 
      InputStream in = ClassLoaderUtil.getResourceAsStream(page.substring(indexOf+1), getClass()); 
                        page = page.replace("//", "/"); 

                        if (in == null) { 
                                in = servletContext.getResourceAsStream(page); 
                                while (in == null && page.indexOf('/', 1) > 0) { 
                                        page = page.substring(page.indexOf('/', 1)); 
                                        in = servletContext.getResourceAsStream(page); 
                                } 
                        } 
                        pageLines = read(in, -1); 

                        if (in != null) { 
                                in.close(); 
                        } 
                } 

                if (className != null && className.trim().length() > 0) { 
                        className = "/"+className.replace('.', '/') + ".java"
                        InputStream in = getClass().getResourceAsStream(className); 
                        if (in == null) { 
                                in = servletContext.getResourceAsStream("/WEB-INF/src"+className); 
                        } 
                        classLines = read(in, -1); 

                        if (in != null) { 
                                in.close(); 
                        } 
                } 

                String rootPath = ServletActionContext.getServletContext().getRealPath("/"); 
                                 
                if (config != null && config.trim().length() > 0 && (rootPath == null || config.startsWith(rootPath))) { 
                        int pos = config.lastIndexOf(':'); 
                        configLine = Integer.parseInt(config.substring(pos+1)); 
                        config = config.substring(0, pos).replace("//", "/"); 
                        configLines = read(new URL(config).openStream(), configLine); 
                } 
                return SUCCESS; 
        } 


        /** 
         * @param className the className to set 
         */
 
        public void setClassName(String className) { 
                this.className = className; 
        } 

        /** 
         * @param config the config to set 
         */
 
        public void setConfig(String config) { 
                this.config = config; 
        } 

        /** 
         * @param page the page to set 
         */
 
        public void setPage(String page) { 
                this.page = page; 
        } 

        /** 
         * @param padding the padding to set 
         */
 
        public void setPadding(int padding) { 
                this.padding = padding; 
        } 



        /** 
         * @return the classLines 
         */
 
        public List getClassLines() { 
                return classLines; 
        } 

        /** 
         * @return the configLines 
         */
 
        public List getConfigLines() { 
                return configLines; 
        } 

        /** 
         * @return the pageLines 
         */
 
        public List getPageLines() { 
                return pageLines; 
        } 

        /** 
         * @return the className 
         */
 
        public String getClassName() { 
                return className; 
        } 

        /** 
         * @return the config 
         */
 
        public String getConfig() { 
                return config; 
        } 

        /** 
         * @return the page 
         */
 
        public String getPage() { 
                return page; 
        } 

        /** 
         * @return the configLine 
         */
 
        public int getConfigLine() { 
                return configLine; 
        } 

        /** 
         * @return the padding 
         */
 
        public int getPadding() { 
                return padding; 
        } 

        /** 
         * Reads in a strea, optionally only including the target line number 
         * and its padding 
         * 
         * @param in The input stream 
         * @param targetLineNumber The target line number, negative to read all 
         * @return A list of lines 
         */
 
        private List read(InputStream in, int targetLineNumber) { 
                List snippet = null
                if (in != null) { 
                        snippet = new ArrayList(); 
                        int startLine = 0; 
                        int endLine = Integer.MAX_VALUE; 
                        if (targetLineNumber > 0) { 
                                startLine = targetLineNumber - padding; 
                                endLine = targetLineNumber + padding; 
                        } 
                        try { 
                                BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 

                                int lineno = 0; 
                                String line; 
                                while ((line = reader.readLine()) != null) { 
                                        lineno++; 
                                        if (lineno >= startLine && lineno <= endLine) { 
                                                snippet.add(line); 
                                        } 
                                } 
                        } catch (Exception ex) { 
                                // ignoring as snippet not available isn't a big deal 
                        } 
                } 
                return snippet; 
        } 

        public void setServletContext(ServletContext arg0) { 
                this.servletContext = arg0; 
        } 



 


本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/855193,如需转载请自行联系原作者

相关文章:

  • chomperwu
  • 在SpringMVC中使用拦截器(interceptor)拦截CSRF攻击
  • 一、网络的基本概念
  • 利用LVS-NAT和DR模型分别负载均衡一个php应用
  • VII Python(9)socket编程
  • zookeeper系列(七)实战分布式命名服务
  • Animations的使用
  • 利用Python生成随机4位验证码
  • 测试标准学习
  • 一些不常见的css知识
  • 第四课——MFC应用程序框架
  • 列表、元祖概述
  • 拨云见日—深入解析Oracle TX 行锁(上)
  • 询问Spring Bott和高并发框架两个问题
  • 用 vue 组件自定义 v-model, 实现一个 Tab 组件。
  • 〔开发系列〕一次关于小程序开发的深度总结
  • avalon2.2的VM生成过程
  • C++类的相互关联
  • CSS实用技巧
  • Docker下部署自己的LNMP工作环境
  • idea + plantuml 画流程图
  • java8-模拟hadoop
  • JavaScript 事件——“事件类型”中“HTML5事件”的注意要点
  • Java应用性能调优
  • jdbc就是这么简单
  • js
  • Laravel深入学习6 - 应用体系结构:解耦事件处理器
  • linux安装openssl、swoole等扩展的具体步骤
  • nginx(二):进阶配置介绍--rewrite用法,压缩,https虚拟主机等
  • ReactNativeweexDeviceOne对比
  • spark本地环境的搭建到运行第一个spark程序
  • Sublime text 3 3103 注册码
  • text-decoration与color属性
  • 创建一种深思熟虑的文化
  • 构建二叉树进行数值数组的去重及优化
  • 使用docker-compose进行多节点部署
  • 问题之ssh中Host key verification failed的解决
  • 在 Chrome DevTools 中调试 JavaScript 入门
  • 继 XDL 之后,阿里妈妈开源大规模分布式图表征学习框架 Euler ...
  • ​软考-高级-系统架构设计师教程(清华第2版)【第1章-绪论-思维导图】​
  • # 数据结构
  • #我与Java虚拟机的故事#连载12:一本书带我深入Java领域
  • (9)STL算法之逆转旋转
  • (zt)最盛行的警世狂言(爆笑)
  • (超简单)使用vuepress搭建自己的博客并部署到github pages上
  • (分享)自己整理的一些简单awk实用语句
  • (附源码)python旅游推荐系统 毕业设计 250623
  • (附源码)ssm本科教学合格评估管理系统 毕业设计 180916
  • (十一)c52学习之旅-动态数码管
  • (转)树状数组
  • (转载)微软数据挖掘算法:Microsoft 时序算法(5)
  • ***php进行支付宝开发中return_url和notify_url的区别分析
  • .MSSQLSERVER 导入导出 命令集--堪称经典,值得借鉴!
  • .net core MVC 通过 Filters 过滤器拦截请求及响应内容
  • .net core Swagger 过滤部分Api