Struts查看文件内容功能的方法

来源:java认证发布时间:2012-11-12 13:16:20java认证视频

  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);
  }
  }
  }

  考试大温馨提示:本内容来源于网络,仅代表作者个人观点,与本站立场无关,仅供您学习交流使用。其中可能有部分文章经过多次转载而造成文章内容缺失、错误或文章作者不详等问题,请您谅解。如有侵犯您的权利,请联系我们,本站会立即予以处理。

上一页12下一页

视频学习

我考网版权与免责声明

① 凡本网注明稿件来源为"原创"的所有文字、图片和音视频稿件,版权均属本网所有。任何媒体、网站或个人转载、链接转贴或以其他方式复制发表时必须注明"稿件来源:我考网",违者本网将依法追究责任;

② 本网部分稿件来源于网络,任何单位或个人认为我考网发布的内容可能涉嫌侵犯其合法权益,应该及时向我考网书面反馈,并提供身份证明、权属证明及详细侵权情况证明,我考网在收到上述法律文件后,将会尽快移除被控侵权内容。

最近更新

社区交流

考试问答