struts2下载以及文件未找到提示

本文介绍如何使用Struts2框架实现文件的上传与下载功能。详细展示了JSP页面的表单设置、Struts配置文件中对于下载Action的配置方式,以及必需的Java代码实现。包括了设置文件类型、下载文件名的转码处理等关键步骤。

 

jsp页面
	<form action="<c:url value='/uploadFile.do'/>" name="form1" method="post"
						enctype="multipart/form-data" >
          <input name="doc" id= "doc" type="file" />
</form>

其中: enctype="multipart/form-data"   必备

 

 

struts.xml配置:
<action name="download" class="uploadandDownloadRiskReportAction">
	 <param name="fileName">此入填写下提示的名称,比如:测试报告.doc</param>
	<result name="success" type="stream">
		<param name="contentType">application/octet-stream;charset=ISO8859-1</param>
		<param name="inputName">inputStream</param>
			<!-- 使用经过转码的文件名作为下载文件名,downloadFileName属性	对应action类中的方法 getDownloadFileName() -->
		<param name="contentDisposition">attachment;filename="${downloadFileName}"</param>
				<param name="bufferSize">4096</param>
	</result>
                 <result name="error">/app/report/annualReportOfTheLegalRisks/error.jsp</result>
</action>

 

JAVA 代码: 
private String fileName;// 初始的通过param指定的文件名属性  set/get省略     
public InputStream getInputStream()  {
             //下载地址
		String inputPath = this.getRequest().getRealPath("/")
				+ "docs\\annualreport\\template\\测试报告.doc";
		logger.info("inputPath ***********" + inputPath);
		InputStream stream = null;
		try {
			stream = new java.io.FileInputStream(inputPath);
		} catch (FileNotFoundException e) {
		
		}catch(IOException ie){
			
		}
		return stream;
	}
public String execute(){
  logger.info("****************************会先*进入了execute 方法*******************************");
  String inputPath = this.getRequest().getRealPath("/")+PropertiesUtil.getInstance().getProperty("riskReportFilePath");
  File file = new File(inputPath);
  if(file.exists()){
   return SUCCESS;
  }else{
    this.errorStr ="未找到文件";
    this.returnUrl="/uploadandDownload/init.do";
    return ERROR;
  }
 }
	public String getDownloadFileName() {
		String downFileName = fileName;

		try {
			downFileName = new String(downFileName.getBytes(), "ISO8859-1");
		} catch (UnsupportedEncodingException e) {
		}
		return downFileName;
	}

java中的两个方法必备。

 具体查看资料:http://berry0606.iteye.com/blog/570239

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值