我們經常遇到,檢視檔案源代碼的功能。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);
in = servletcontext.getresourceasstream("/web-inf/src"+classname);
classlines = read(in, -1);
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;
}