struts2实现文件上传下载

Posted 输入密码

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
    }
}

 

以上是关于struts2实现文件上传下载的主要内容,如果未能解决你的问题,请参考以下文章

Struts2学习—文件上传和下载

Struts2 + MySQL 实现文件上传

Struts2(八.添加用户多张照片实现文件上传功能)

用struts2实现文件的上传下载

基于Struts2框架的文件下载 --- Struts2

jquery+struts2 上传文件