zoukankan      html  css  js  c++  java
  • Struts2文件上传和下载

    1.文件上传

    jsp页面

     1 <input name="packagePath" type="file"> 

    action

     1     private File packagePath;//文件上传属性
     2     private String packagePathContentType;//文件上传类型
     3     private String packagePathFileName;//文件上传名称
     4     private String savePath;//文件上传路径
     5 
     6     /**
     7      * 新增
     8      * @return
     9      * @throws Exception
    10      */
    11     public String add() throws Exception {
    12         
    13         saveFile();//上传附件
    14         
    15         return "add";
    16     }
    17 
    18     private void saveFile() throws Exception{
    19         byte[] buffer =new byte[1024];
    20         FileInputStream fis=new FileInputStream(getPackagePath());//读取文件
    21         //保存文件,并设置保存目录的路径
    22         FileOutputStream fos=new FileOutputStream(getSavePath()+"\"+this.getPackagePathFileName());
    23         int length=fis.read(buffer);
    24         while(length>0){
    25             fos.write(buffer,0,length);
    26             length=fis.read(buffer);
    27         }
    28         fis.close();
    29         fos.flush();
    30         fos.close();
    31     }
    32 
    33     public String getSavePath() {
    34         return ServletActionContext.getServletContext().getRealPath(savePath);
    35     }
    36 
    37 
    38     public void setSavePath(String savePath) {
    39         this.savePath = savePath;
    40     }

    struts.xml

    1         <action name="stu*" class="updateStandardAction" method="{1}">
    2             <param name="savePath">/attachment</param> //在项目中要有attachment文件夹,用于保存上传的文件
    3         </action>

    下载文件

    jsp页面

     1 <a href="download.action?fileName=${p.packagePath}">下载</a> 

    action

     1 package cn.bd.standard.action;
     2 
     3 import java.io.BufferedInputStream;
     4 import java.io.FileInputStream;
     5 import java.io.FileNotFoundException;
     6 import java.io.InputStream;
     7 import java.io.UnsupportedEncodingException;
     8 
     9 import org.apache.struts2.ServletActionContext;
    10 
    11 import com.opensymphony.xwork2.ActionSupport;
    12 /**
    13  * 下载
    14  * @author TaoXianXue
    15  *
    16  */
    17 public class AttachmentDownAction extends ActionSupport{
    18     /**
    19      * 
    20      */
    21     private static final long serialVersionUID = 1L;
    22     
    23     
    24     private String inputPath;//读取下载文件的目录
    25     private String fileName;//下载文件的文件名
    26     private InputStream inputStream;//读取下载文件的输入流
    27     private String contentType;//下载文件的类型
    28     
    29     @Override
    30     public String execute() throws Exception {
    31         // TODO Auto-generated method stub
    32         return super.execute();
    33     }
    34     
    35     public String getInputPath() {
    36         return inputPath;
    37     }
    38     public void setInputPath(String inputPath) {
    39         this.inputPath = inputPath;
    40     }
    41     public String getFileName() throws UnsupportedEncodingException {
    42         return java.net.URLEncoder.encode(fileName, "UTF-8");//??????????????????
    43         /*return fileName;*/
    44     }
    45     public void setFileName(String fileName)  {
    46         /*this.fileName = new String(fileName.getBytes("ISO8859-1"), "utf-8");*/
    47         this.fileName=fileName;
    48     }
    49     public InputStream getInputStream() throws FileNotFoundException {
    50         String path=ServletActionContext.getServletContext().getRealPath(inputPath);
    51         return new BufferedInputStream(new FileInputStream(path+"\"+fileName));
    52     }
    53     public void setInputStream(InputStream inputStream) {
    54         this.inputStream = inputStream;
    55     }
    56     public String getContentType() {
    57         return contentType;
    58     }
    59     public void setContentType(String contentType) {
    60         this.contentType = contentType;
    61     }
    62 }

    struts.xml

    1         <action name="download" class="cn.bd.standard.action.AttachmentDownAction">
    2             <param name="inputPath">/attachment</param> <!-- 从那里下载 -->
    3             <result name="success" type="stream">
    4                 <param name="contentType">application/octet-stream</param><!-- 下载的文件类型 -->
    5                 <param name="inputName">inputStream</param>
    6                 <param name="contentDisposition">attachment;fileName="${fileName}"</param>
    7             </result>
    8         </action>
  • 相关阅读:
    20200804 千锤百炼软工人第三十天
    20200803 千锤百炼软工人第二十九天
    20200802 千锤百炼软工人第二十八天
    小谢第51问:从输入url到浏览器显示页面发生了什么
    小谢第50问:vuex的五个属性-使用-介绍
    小谢第49问:URL都由什么组成
    小谢第48问:js跳转页面与打开新窗口的方法
    小谢第47问:vue项目中,assets和static的区别
    小谢第46问:js事件机制
    小谢第45问:Ajax 是什么? 如何创建一个 Ajax
  • 原文地址:https://www.cnblogs.com/taobd/p/6682618.html
Copyright © 2011-2022 走看看