Struts数据的上传与下载

来源:互联网 发布:起点网络写手新手收入 编辑:程序博客网 时间:2024/06/05 09:40


文件的上传


步骤一:导入需要的jar包



步骤二:建个上传的界面~~


步骤三:配置上传的FileUploadAction活动类~


public class FileUploadAction extends ActionSupport { private File file1; private String file1FileName; private String file1ContentType;  public void setFile1(File file1) {this.file1 = file1;}public void setFile1FileName(String file1FileName) {this.file1FileName = file1FileName;}public void setFile1ContentType(String file1ContentType) {this.file1ContentType = file1ContentType;} @Overridepublic String execute() throws Exception {//获取上传路径String path=ServletActionContext.getServletContext().getRealPath("/upload");//创建目标文件对象System.out.println(path);File destFile=new File(path,file1FileName);System.out.println("测试");//把上传的文件拷贝到目标文件中FileUtils.copyFile(file1, destFile);   return "success";}}


步骤四:配置FileUploadAction活动类相对于的xml配置文件(别忘了web.xml中配置核心过滤器)

  

 <action name="fileUploadActions" class="cn.itcast.fileupload.FileUploadAction">      <interceptor-ref name="defaultStack">   <!-- 限制上传文件的扩展名 -->  <param name="fileUpload.allowedExtensions">txt,jpg,jar</param>   <!-- 限制运行的类型【与上面的同时使用,取交集】 --> <!--    <param name="fileUpload.allowedExtensions">txt,jpeg</param>-->   </interceptor-ref>        <result name="success">/E/success.jsp</result>    <!-- 错误页面的返回值是input -->    <result name="input">/E/error.jsp</result> </action>

运行一下~

 



文件下载


步骤一:建立文件下载需要的list界面


<body>  <fieldSet>   <table border="1">   <tr>      <th>序号</th>      <th>文件名</th>      <th>操作</th>     </tr>   <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>     <c:forEach varStatus="vs" var="fileName" items="${fileNames}">      <tr>        <td>${vs.count}</td>        <td>${fileName}</td>        <td>         <!-- 构建一个url -->         <c:url var="url" value="down_down">           <c:param name="fileName" value="${fileName }"></c:param>         </c:url>         <a href="${url}">下载</a>        </td>      </tr>     </c:forEach>   </table>   </fieldSet>  </body>


步骤二:建立DownAction活动类

public class DownAction extends ActionSupport{/*****获取下载列表****/public String list() throws Exception{//得到upload的目录路径      String path=ServletActionContext.getServletContext().getRealPath("/upload");//目录对象 File file=new File(path);      //得到所有要下载的文件的文件名 String []fileNames=file.list();//保存 ActionContext ac=ActionContext.getContext();//得到request的map(第二种方式)  Map<String,Object> request=(Map<String,Object>)ac.get("request");request.put("fileNames", fileNames);return "list";} /******文件下载*****///获取要下载的文件名private String fileName;public void setFileName(String fileName) {//因为是url是用get方式提交,所以要处理中文乱码问题try {fileName=new String(fileName.getBytes("ISO8859-1"),"UTF-8");} catch (UnsupportedEncodingException e) {e.printStackTrace();}this.fileName = fileName;}//2. 下载提交的业务方法 (在struts.xml中配置返回stream)public String down()throws Exception{return "download";}//3,返回文件流public InputStream  getAttrInputStream(){return ServletActionContext.getServletContext().getResourceAsStream("/upload/"+fileName);}//.使下载文件的名字为显示的中文名  与upload.xml中的downFileName相对应  public String getDownFileName(){ try {fileName=URLEncoder.encode(fileName, "UTF-8");} catch (UnsupportedEncodingException e) {e.printStackTrace();}return fileName;}}

步骤三:在相对应的xml文件中注册活动(别忘了web.xml中配置核心过滤器)

<action name="down_*" class="cn.itcast.fileupload.DownAction" method="{1}">     <result name="list">/E/list.jsp</result>         <result name="download" type="stream">           <!-- 下载文件类型:指定为二进制文件类型 -->      <param name="contentType">application/octet-stream</param>      <!-- 对应Action中的属性,返回流的属性【就是getAttrInputStream()】 -->      <param name="inputName">attrInputStream</param>      <!-- 下载头,包括:浏览器显示的文件名 -->      <param name="contentDisposition">attachment;filename=${downFileName}</param>      <!-- 缓冲区设y置大小 -->     <param name="bufferSize">1024</param>     </result>   </action>




原创粉丝点击