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

huihui0929 2011-03-09 05:08:10
麻烦大家帮我看看,在网上搜索了一下,但是都没有结果!
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.

这个异常,麻烦大家帮忙看看,谢谢了!
...全文
567 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
困井 2011-05-12
  • 打赏
  • 举报
回复
请把代码写全
huihui0929 2011-03-10
  • 打赏
  • 举报
回复
你说的不对啊,还是出现这个问题
yujinjin9 2011-03-10
  • 打赏
  • 举报
回复
filepaths,filenames的获取都是客户端的文件路径、名称肯定会出错了!这些都是本地绝对路径呀!
redlotus_lyn 2011-03-09
  • 打赏
  • 举报
回复
<param name="inputName">${downloadFile}</param>
huihui0929 2011-03-09
  • 打赏
  • 举报
回复
分两部分代码,一部分是action的代码,一部分是struts-config.xml的代码。
action中的代码比较长,麻烦大家帮忙看看了,谢谢了!

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧