• 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;
        }
    }
  • 相关阅读:
    单例模式
    二、CSS
    十一、多线程
    十二、协程
    十、多进程
    九、内存管理
    八、元类
    七、上下文管理器/魔术方法
    六、单例模式
    五、装饰器
  • 原文地址:https://www.cnblogs.com/jiaobaobao/p/9565195.html
Copyright © 2020-2023  润新知