struts2实现文件上传下载

代码比较粗糙,这里只做个记录,方便日后查询

 

/**
 * 上传文件代码
 */

public class UploadAndDownloadAction extends ActionSupport{ 

    // 获取文件
    private File myFile;
    // 获取文件的名称
    private String myFileFileName;
    // 获取文件的类型
    private String myFileContentType;
    public File getMyFile() {
        return myFile;
    }
    public void setMyFile(File myFile) {
        this.myFile = myFile;
    }
    public String getMyFileFileName() {
        return myFileFileName;
    }
    public void setMyFileFileName(String myFileFileName) {
        this.myFileFileName = myFileFileName;
    }
    public String getMyFileContentType() {
        return myFileContentType;
    }
    public void setMyFileContentType(String myFileContentType) {
        this.myFileContentType = myFileContentType;
    }
    
    
    public boolean upload() {
            
            // 1.确定上传的文件保存的位置
             String uploadRootPath = "/common/download/template";
             String dir = Struts2Util.getRequest().getRealPath(uploadRootPath);
            // 2.判断这个文件夹是否存在,不存在创建
            File file = new File(dir);
            if (!file.exists()) {
                file.mkdir();
            }
            // 3.将文件放到InputStream里面,然后outputstream写入文件
            InputStream is = null;
            OutputStream os = null;
            try {
                is = new BufferedInputStream(new FileInputStream(myFile));
                os = new BufferedOutputStream(new FileOutputStream(dir + "\\" + myFileFileName));
                byte[] buffer = new byte[1024];
                int byteLen = 0;
                while ((byteLen = is.read(buffer)) > 0) {
                    os.write(buffer, 0, byteLen);
                }
                domain.setTemplateName(myFileFileName);
                domain.setTemplateUrl(uploadRootPath+ "\\" + myFileFileName);
                domain.setTemplateFullPath(dir+ "\\" + myFileFileName);
                
            } catch (Exception e) {
                
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                    }
                }
                if (os != null) {
                    try {
                        os.close();
                    } catch (IOException e) {
                    }
                }
            }
            return true;
        }
}
/**
 *  文件下载
 */

public class UploadAndDownloadAction extends ActionSupport  { 

    
    
    private String fileName;
    
    public String getFileName() {
         try{
             fileName = URLEncoder.encode(fileName,"UTF-8");
         }catch(Exception e){
             throw new RuntimeException();
         }
         
         return fileName;
    }
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }


     
    //2.3:返回流的方法 下载
     public InputStream getAttrInputStream() throws ItsException, UnsupportedEncodingException{
         String fileName=Struts2Util.getRequest().getParameter("fileName");
             this.setFileName(fileName);
             InputStream is=ServletActionContext.getServletContext().getResourceAsStream("路径");
             return is;
         }else{
             return null;
         }
     }
          
     public String getDownloadFile() throws ItsException {
         return "success";
     }
     
     /**
      *  struts2 配置
      */
     <action name="uploadAndDownload*" class="uploadAndDownloadAction" method="{1}">
            <result name="uploadTemplate">/common/download/jsp/uploadTemplate.jsp</result>
            <!-- 下载操作 -->
              <result name="success" type="stream">
                 <param name="contentType">application/octet-stream</param>
                 <param name="inputName">attrInputStream</param>
                 <param name="contentDisposition">attachment;filename="${fileName}"</param>
                 <param name="bufferSize">1024</param>          
              </result>
    </action>


}
/**
 * 删除文件
 */

public static boolean delete(String path) {
    File file = new File(path);
    if (!file.exists()) {
        return false;
    }else{
        return deleteFile(path);
    }
}
/**
 * 删除文件
 */
public static boolean deleteFile(String path) {
    File file = new File(path);
    // 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
    if (file.delete()) {
        return true;
    }else {
        return false;
    }
}

 

转载于:https://www.cnblogs.com/jiaobaobao/p/9565195.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值