WebWork下载文件时,首先在xwork.xml中添加以下内容:
- <action name="download" class="com.mycompany.action.FileDownloadAction">
- <result name="success" type="stream">
- <param name="contentType">application/octet-stream</param>
- <param name="inputName">inputStream</param>
- <param name="contentDisposition">attachment;filename="${downloadFileName}"</param>
- <param name="bufferSize">4096</param>
- </result>
- </action>
- <action name="download" class="com.mycompany.action.FileDownloadAction">
- <result name="success" type="stream">
- <param name="contentType">application/octet-stream</param>
- <param name="inputName">inputStream</param>
- <param name="contentDisposition">attachment;filename="${downloadFileName}"</param>
- <param name="bufferSize">4096</param>
- </result>
- </action>
这时需要把fileName作为参数传过来,以下是action:
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.InputStream;
- import java.io.UnsupportedEncodingException;
- import com.opensymphony.xwork.Action;
- public class FileDownloadAction implements Action {
- private String fileName = "";
- public InputStream getInputStream () throws Exception {
- InputStream is = null;
- try {
- is = new FileInputStream(fileName);
- } catch (FileNotFoundException e1) {
- }
- return is;
- }
- public String execute () throws Exception {
- return SUCCESS;
- }
- public void setFileName (String fileName) {
- this.fileName = fileName;
- }
- public String getFileName () {
- return fileName;
- }
- public String getDownloadFileName () {
- String downFileName = fileName;
- try {
- downFileName = new String(downFileName.getBytes(), "utf8");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- return downFileName;
- }
- }
至此就完成了WebWork下载。
【编辑推荐】