主要实现步骤:
1.先把input标签的type改为file,然后跳转到相应的action,action里面将你上传的文档放在相应的文件夹下面,然后跳转到成功的页面showc.jsp。
2.在showc.jsp上面提供相应的下载地址,完成下载操作。
主要实现过程:
struts.xml:配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <!-- 当struts的配置文件修改后,系统是否自动重新加载 该文件,默认值为false(生产环境下使用),开发阶段最好打开 --> <constant name="struts.configuration.xml.reload" value="true" /> <package namespace="/" name="default" extends="struts-default"> <action name="DownloadAction" class="cn.hw.action.DownloadAction"> <result name="success" type="stream"> <param name="contentType">text/plain</param> <param name="contentDisposition">attachment;fileName="${fileName}"</param> <param name="inputName">downloadFile</param> <param name="bufferSize">1024</param> </result> </action> <action name="UploadFileAction" class="cn.hw.action.UploadFileAction"> <!-- 将savePath设置为"根目录下的upload文件夹" --> <param name="savePath">/upload</param> <result name="show">/showc.jsp</result> <result name="error">/index.jsp</result> </action> </package> </struts>index.jsp:
<script type="text/javascript"> function checkup(){ //提示信息 var div=document.getElementById("uploadname"); if(document.form1.gljueyi.value.length==0){ div.innerHTML="请选择要上传的文件"; document.form1.gljueyi.focus(); return false; } return true; } </script> <body> <embed height="100" width="100" src="upload/12345.mp3"></embed><span style="color:#ff0000;">注明:此处这个是背景音乐,只需一行代码,src里面的mp3文件的地址</span> <form action="UploadFileAction.action" name="form1" method="post" enctype="multipart/form-data"> <table> <tr> <td > </td> <td ><span id="message" style="font-size:16px;color:red;" > </span></td> </tr> <tr> <td style="line-height:30px;">选择上传的文件:</td> <td> <input type="file" id="uploadname" name="upload"></td> </tr> <tr> <td style="line-height:30px;"><input type="submit" name="button" id="button" value="提交" onClick="return checkup();"></td> <td><input type="reset" name="button2" id="button2" value="重置"></td> </tr> </table> </form> </body> UploadFileAction:(上传action) package cn.hw.action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import org.textmining.text.extraction.WordExtractor; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.sun.jmx.snmp.Timestamp; public class UploadFileAction extends ActionSupport { <span style="white-space:pre"> </span> <span style="white-space:pre"> </span> //创建对象 <span style="white-space:pre"> </span> ActionContext context = ActionContext.getContext(); <span style="white-space:pre"> </span>Map session = context.getSession(); <span style="white-space:pre"> </span> File upload; String uploadContentType; String uploadFileName; String str; private String savePath; //接受依赖注入的方法 public void setSavePath(String value) { this.savePath = value; } @SuppressWarnings("deprecation") private String getSavePath() throws Exception { return ServletActionContext.getRequest().getRealPath(savePath); } public void setUpload(File upload) { this.upload = upload; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; } public void setUploadFileName(String uploadFileName) { this.uploadFileName = uploadFileName; } public File getUpload() { return (this.upload); } public String getUploadContentType() { return (this.uploadContentType); } public String getUploadFileName() { return (this.uploadFileName); } public String getStr() { return str; } public void setStr(String str) { this.str = str; } public String execute() throws Exception{ /* * 文件上传工作 */ String doc = getFilename(getUploadFileName()); uploadFileName =getTime() + "." + doc; System.out.println("名字:"+uploadFileName); String path = ServletActionContext.getServletContext().getRealPath("/upload"); System.out.println("真实的路径:"+path); //新建一个文件夹,然后把文档放在里面 File dir = new File(path); <span style="white-space:pre"> </span>if (!dir.exists()) { <span style="white-space:pre"> </span>dir.mkdir(); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>FileUtils.copyFile(upload, new File(dir, uploadFileName)); <span style="white-space:pre"> </span>//把文件的名字放在session里面,返回到页面上。 <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>session.put("uploadFileName", uploadFileName); return "show"; } String getTime(){ /** * 获取时间来定义文件名称 * @return String as the name of the file */ SimpleDateFormat formatter_f = new SimpleDateFormat("yyyyMMddHHmmss"); Date now = new Date(); String time = formatter_f.format(now); return time; } String getTrueTime(){ /** * 获取当前时间 * @return String time */ SimpleDateFormat formatter_f = new SimpleDateFormat("yyyy-MM-dd"); Date now = new Date(); String time = formatter_f.format(now); return time; } String getFilename(String name){ /** * 获取文件名的后缀 * @return String */ int i = name.lastIndexOf(".")+1; return name.substring(i,name.length()); } } showc.jsp:(成功之后的页面)<body> 上传成功啦 文件名字:${session.uploadFileName} <a href="DownloadAction.action">我要下载我要下载</a> </body>
DownloadAction:(下载action) package cn.hw.action; import java.io.InputStream; import java.util.Map; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class DownloadAction extends ActionSupport { //创建对象 ActionContext context = ActionContext.getContext(); Map session = context.getSession(); private String fileName; public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } // 返回一个输入流,作为一个客户端来说是一个输入流,但对于服务器端是一个 输出流 public InputStream getDownloadFile() throws Exception { this.fileName=(String) session.get("uploadFileName"); //this.fileName = "1.docx" ; // 解解乱码 this.fileName = new String(this.fileName.getBytes("GBK"),"ISO-8859-1"); return ServletActionContext.getServletContext().getResourceAsStream("upload/"+this.fileName) ; } @Override public String execute() throws Exception { return SUCCESS; } }
上传页面:
下载页面:
