Struts 查看文件内容功能

简介:
1.Action 代码
/* 
* $Id: ShowFileAction.java 471754 2006-11-06 14:55:09Z husted $ 

* 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.struts.webapp.validator; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import java.io.InputStream; 
import java.io.InputStreamReader; 

import org.apache.struts.action.Action; 
import org.apache.struts.action.ActionForm; 
import org.apache.struts.action.ActionForward; 
import org.apache.struts.action.ActionMapping; 
import org.apache.commons.logging.LogFactory; 
import org.apache.commons.logging.Log; 

/** 
* Action which retrieves a file specified in the parameter 
* and stores its contents in the request, so that they 
* can be displayed. 
*/
 
public  class ShowFileAction  extends Action { 

         /** Logging Instance. */ 
         private  static  final Log log = LogFactory.getLog(ShowFileAction. class); 

         public ActionForward execute(ActionMapping mapping, 
                                                                 ActionForm form, 
                                                                 HttpServletRequest request, 
                                                                 HttpServletResponse response) 
                                                     throws Exception { 

                 // Get the file name 
                String fileName = mapping.getParameter(); 
                StringBuffer fileContents =  new StringBuffer(); 

                 if(fileName !=  null) { 

                        InputStream input = servlet.getServletContext().getResourceAsStream(fileName); 
                         if (input ==  null) { 
                                log.warn( "File Not Found: "+fileName); 
                        }  else { 
                                InputStreamReader inputReader =  new InputStreamReader(input); 
                                 char[] buffer =  new  char[1000]; 
                                 while ( true) { 
                                         int lth = inputReader.read(buffer); 
                                         if (lth < 0) { 
                                                 break
                                        }  else { 
                                                fileContents.append(buffer, 0, lth); 
                                        } 
                                } 
                        } 
                }  else { 
                        log.error( "No file name specified."); 
                } 


                 // Store the File contents and name in the Request 
                request.setAttribute( "fileName", fileName); 
                request.setAttribute( "fileContents", fileContents.toString()); 

                 return mapping.findForward( "success"); 
        } 

分析:
 
 String fileName = mapping.getParameter();
其中mapping 是ActionMapping 对象,是ActionConfig的子对象。 其中ActionConfig封装了Struts-config.xml 中的配置信息。
 
inputStream input = servlet.getServletContext().getResourceAsStream(fileName);
每个Web应用程序都是一个独立的 Servlet容器,每个Web应用程序分别用一个ServletContext对象。ServletContext对象包含在ServletConfig对象中,
调用ServletConfig. getServletContext()方法获取ServletContext对象。
1、    getResourcePath    返回一个包含该目录和文件路径名称的Set集合
2、    getResource        返回映射到资源上的URL对象。
3、     getResourceAsStream 返回连接到某资源上的InputStream对象
 
 
 InputStreamReader inputReader = new InputStreamReader(input);
需要重新包装成字符处理。
 
【2】配置文件
        <!-- Show validations.xml --> 
        <action path= "/showValidation"    
                        type= "org.apache.struts.webapp.validator.ShowFileAction"    
                        scope= "request"    
                        parameter= "/WEB-INF/validator/validation.xml"
                <forward name= "success" path= "/showFile.jsp" /> 
        </action>
 
通过传递不同的parameter,读取不同的文件。
 
【3】在JSP页面 读取信息
    <body bgcolor= "white"

        <h2>File: <i><bean:write name= "fileName" scope= "request" /></i></h2> 
        <hr /> 
        <pre> 
                <bean:write name= "fileContents" scope= "request" filter= "true"/> 
        </pre> 
        <hr /> 
    </body>
 
bean:write 标签 有个filter属性。如果为true 的话,则表示
将把输出内容中的特殊HTML符号作为普通字符串来显示;如果 filter属性为false,则不会把输出内容中的HTML符号转化为普通字符串. 



本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/241235,如需转载请自行联系原作者
相关文章
|
4月前
|
XML C# 图形学
【Unity 3D】C#从XML中写入、读取、修改数据(附源码)
【Unity 3D】C#从XML中写入、读取、修改数据(附源码)
33 0
|
XML JavaScript 前端开发
具备spring环境的测试头注解;xml 规则;JavaScript:改变 HTML 内容案例
具备spring环境的测试头注解 具备spring环境的测试头注解 test
119 1
具备spring环境的测试头注解;xml 规则;JavaScript:改变 HTML 内容案例
|
XML JavaScript Java
Java Web之JSP操作XML(XML的文档结构 语法和注释、dom4j的下载与配置 应用dom4j创建、解析和修改XML)
Java Web之JSP操作XML(XML的文档结构 语法和注释、dom4j的下载与配置 应用dom4j创建、解析和修改XML)
185 0
Java Web之JSP操作XML(XML的文档结构 语法和注释、dom4j的下载与配置 应用dom4j创建、解析和修改XML)
|
XML C# 数据格式
C# XML基础入门(XML文件内容增删改查清)
C# XML基础入门(XML文件内容增删改查清)
139 0
|
XML Java Apache
配置struts-2.5.16必需jar包Struts2的特性变动
1、jar包的变动 必需jar包,旧版本:    必需jar包,新版本:    在struts-2.5.16版本的lib目录下没有xwork-core的jar包,原因是被合并到struts-core这个jar里了。
2004 0
|
JSON Java 数据格式
struts2框架单文件、多文件上传实例详解
版权声明:本文为博主原创文章,如需转载,请标明出处。 https://blog.csdn.net/alan_liuyue/article/details/79390681 简介  ...
1089 0
|
Java 前端开发 应用服务中间件