首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

关于struts2 批量下载文件,代码写好了,但是出现错误,严重: Can not find a java.io.InputStream with the na

2012-03-23 
关于struts2 批量下载文件,代码写好了,但是出现异常,严重: Can not find a java.io.InputStream with the

关于struts2 批量下载文件,代码写好了,但是出现异常,严重: Can not find a java.io.InputStream with the na
麻烦大家帮我看看,在网上搜索了一下,但是都没有结果!
action代码:
  package cn.edu.cuit.disasterSystem.web.struts2.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;


import org.apache.struts2.ServletActionContext;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

import com.opensymphony.xwork2.ActionSupport;


@SuppressWarnings("serial")
public class DownloadAction extends ActionSupport {

  private String filenames;
  private String filepaths;
  private String[] filenameArray = null;
  private String[] filepathArray = null;
  private String filename;
  private String filepath;
  private SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
   
   
 
  public String getFilenames() {
  return filenames;
  }
 
  public void setFilenames(String filenames) {
  this.filenames = filenames;
  if (this.filenames.contains("|")) {
  parseFilenamesToArray();
  }
  }
 
  public String getFilepaths() {
  return filepaths;
  }

  public void setFilepaths(String filepaths) {
  this.filepaths = filepaths;
  if (this.filepaths.contains("|")) {
  parseFilepathsToArray();
  }
  }
   
  public void parseFilenamesToArray() {
  filenameArray = filenames.split("\\|");
  }

  public void parseFilepathsToArray() {
  filepathArray = filepaths.split("\\|");
  }
   
  
  public String getFilename() {
  try {
  return new String(filename.getBytes(), "ISO-8859-1");
  } catch (UnsupportedEncodingException e) {
  e.printStackTrace();
  return filename;
  }
  }
   
   

  public String getFilepath(){
  return filepath;
  }

  public void initFilename() {
  if(isBaleZip()){
  this.filename = "批量打包下载.zip";
  }else{
  this.filename = getFilenames();
  }
  System.out.println("下载文件名: "+filename);
  }
   
  public void initFilepath() {
  if(isBaleZip()){
  String rootpath = ServletActionContext.getServletContext().getRealPath("/upload/temp");
  String requestip = ServletActionContext.getRequest().getLocalAddr();
  //this.filepath = "c:\\批量打包下载.zip";
  this.filepath = rootpath+"\\"+requestip+"-"+format.format(new Date())+".zip";
  }else{
  this.filepath = getFilepaths();
  }
  System.out.println("下载文件路径: "+filepath);
  }


  public boolean isBaleZip(){
  boolean isZip = false;
  if(this.filenameArray!= null && this.filepathArray!= null && this.filenameArray.length>0 && this.filenameArray.length==this.filepathArray.length){


  isZip = true;
  }
  return isZip;
  }
   
  public void baleZip(String zipFilePath,String[] names,String[] paths) throws IOException{
  File f = new File(zipFilePath);
  f.createNewFile();
  ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f));
  out.putNextEntry(new ZipEntry("/"));
  for(int i=0;i<paths.length;i++){
  out.putNextEntry(new ZipEntry(names[i])); 
  InputStream in =ServletActionContext.getServletContext().getResourceAsStream(paths[i]);
  int b;
  while ((b = in.read()) != -1) {
  out.write(b);
  }
  in.close();
  }
  out.flush();
  out.close();
  }
   
  public InputStream getDownloadFile(){
  initFilename();
  initFilepath();
  InputStream in = null;
  File tempfile = null;
  if(isBaleZip()){
  try {
  baleZip(this.filepath,this.filenameArray,this.filepathArray);
  tempfile = new File(this.filepath);
  in = new FileInputStream(tempfile);
   
  } catch (IOException e) {
  System.out.println(e.getMessage()+" "+"压缩文件出错!!");
  return null;
  } finally{
  if(tempfile.exists()){
  tempfile.delete();
  if(tempfile.exists()){
  System.out.println("------删除临时文件失败-------");
  }else{
  System.out.println("------删除打包产生的临时文件------");
  }
  }
  }
  }else{
  in = ServletActionContext.getServletContext().getResourceAsStream(getFilepath());
  System.out.println(in);
  }
  return in;
  }
   
   
  @Override
  public String execute() throws Exception {
  // 文件下载目录路径
  String downloadDir = "/upload";
  // 发现企图下载不在 /download 下的文件, 就显示空内容
  if (!filepaths.startsWith(downloadDir)) {
  // 可以抛出一些异常信息
  System.out.println("只能下载upload里面的东西,谢谢!");
  return ERROR;
  }
  return SUCCESS;
  }
}

struts-config.xml代码

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>


<constant name="struts.custom.i18n.resources" value="message"></constant>
<constant name="struts.i18n.encoding" value="gbk"></constant>
<constant name="struts.multipart.saveDir" value="/tmp"></constant>
<constant name="struts.multipart.maxSize" value="209715200" />

<package name="struts2" extends="struts-default">

<action name="downloadList"
class="cn.edu.cuit.disasterSystem.web.struts2.action.DownloadListAction">
<result name="success">/downloadList.jsp</result>


<result name="error">/downloadListError.jsp</result>
</action>


<action name="download"
class="cn.edu.cuit.disasterSystem.web.struts2.action.DownloadAction">
<result name="success" type="stream">
<!-- contentType为二进制方式 -->
<param name="contentType">application/octet-stream;charset=ISO8859-1</param>
<!-- attachment属性强调是下载,就不会主动打开,比如图片 -->
<!-- 使用经过转码的文件名作为下载文件名,downloadFileName属性对应action类中的方法 getDownloadFileName() -->
<param name="contentDisposition">
attachment;filename="${downloadFileName}"
  </param>
<param name="inputName">downloadFile</param>
<param name="bufferSize">4096</param>
</result>
<result name="input">/downloadList.jsp</result>
<result name="error">/downloadListError.jsp</result>
</action>
</package>
</struts>


但是出现严重: Can not find a java.io.InputStream with the name [downloadFile] in the invocation stack. Check the <param name="inputName"> tag specified for this action.

这个异常,麻烦大家帮忙看看,谢谢了!

[解决办法]
<param name="inputName">${downloadFile}</param>

[解决办法]
filepaths,filenames的获取都是客户端的文件路径、名称肯定会出错了!这些都是本地绝对路径呀!

热点排行