1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib uri="/struts-tags" prefix="s"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>struts2的一个例子</title> 9 </head> 10 <body> 11 <s:form action="downloadFile.action" method="post" enctype="multipart/form-data"> 12 <s:submit value="下载文件"/> 13 </s:form> 14 15 </body> 16 </html>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 <constant name="struts.devMode" value="true"/> 7 <package name="hello" extends="struts-default" namespace="/"> 8 <action name="downloadFile" class="com.xiaostudy.web.DownloadFile" method="downloadFile"> 9 <result name="success" type="stream"> 10 <param name="inputName">fileInputStream</param> 11 <param name="contentDisposition">attachment;filename=image.jpg</param> 12 <param name="contentType">application/octet-stream</param> 13 </result> 14 </action> 15 </package> 16 </struts>
1 package com.xiaostudy.web; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.InputStream; 7 8 import org.apache.struts2.ServletActionContext; 9 10 import com.opensymphony.xwork2.ActionSupport; 11 12 public class DownloadFile extends ActionSupport { 13 14 public InputStream fileInputStream; 15 public String downloadFile() throws FileNotFoundException { 16 17 String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/files/CSS.jpg"); 18 fileInputStream = new FileInputStream(new File(path)); 19 20 return SUCCESS; 21 } 22 public InputStream getFileInputStream() { 23 return fileInputStream; 24 } 25 public void setFileInputStream(InputStream fileInputStream) { 26 this.fileInputStream = fileInputStream; 27 } 28 29 30 }