码迷,mamicode.com
首页 > Web开发 > 详细

struts2实现文件上传下载

时间:2018-08-31 13:18:45      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:ati   on()   att   adf   cte   文件   roo   nal   contex   

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

 

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

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;
    }
}

 

struts2实现文件上传下载

标签:ati   on()   att   adf   cte   文件   roo   nal   contex   

原文地址:https://www.cnblogs.com/jiaobaobao/p/9565195.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!