struts入门训练 文件下载

来源:互联网 发布:淘宝神笔我的模块批量 编辑:程序博客网 时间:2024/04/29 21:28

simpledownload.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body>  <a href="simpledownload">Struts.txt</a></body></html>

struts.xml中关于下载文件的参数配置

 <action name="simpledownload" class="com.Three.SimpleDownLoadAction">         <result type="stream">           <!-- 文件类型 -->           <param name="contentType">text/plain</param>           <!-- 指定文件名 -->           <param name="contentDisposition">attachment;filename="Struts.txt"</param>           <!-- 输入流 -->           <param name="inputName">downloadFile</param>         </result>      </action>


SimpleDownLoadAction.java

package com.Three;import java.io.InputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class SimpleDownLoadAction extends ActionSupport{   public InputStream getDownloadFile(){   return ServletActionContext.getServletContext()   .getResourceAsStream("/upload/Struts.txt");   }   @Override   public String execute() throws Exception{   return SUCCESS;   }}

简单的文件下载就完成了,动手试一下吧。

0 0
原创粉丝点击