文件下载

 以下利用Commons-fileupload完成文件的下载 

1.在index.jsp编写代码 

<a href="testDown?fileFileName=upload/1.jpg" target="_blank">down</a>

  2.在struts2.xml配置文件下载

<struts>   
   <package name="team" extends="struts-default" namespace="/">		  
	<!--down-->
	<action name="testDown" class="com.iteye.action.TestAction" method="download">
	  <result type="stream">
	      <param name="contentType">application/octet-stream</param>
	      <param name="inputName">fileDownStream</param>
	      <param name="contentDisposition">attachment;filename=${fileFileName}</param>
	   </result>		       
	</action>	   	   
   </package>
</struts>    

  3.在Action完成文件的下载

public class TestAction extends ActionSupport{
	private String fileFileName;
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	/**
	 * 下载
	 */
	public String download(){
		return SUCCESS;
	}
	//执行下载返回流
	public InputStream getFileDownStream(){		
	   return ServletActionContext.getServletContext().getResourceAsStream(fileFileName);
	}
}

 

你可能感兴趣的:(文件下载)