SSH框架上传附件及下载功能的实现

使用struts2中的文件上传与下载功能,需要先导入两个jar文件,一个是commons-fileupload.jar,另一个是commons-io.jar。
struts2单文件上传:

jsp页面:

     
    <form id="newspost" method="post" action="newspost" namespace="/" enctype="multipart/form-data">
    上传文件:<input type="file" name="file">
    <br>
    <input type="button" value="保存" onclick="subm()">input>
    form>
    <script type="text/javascript">
        function subm(){
                document.getElementById('newspost').submit(); 
           }
    script>

UserArticleAction.java

public class UserArticleAction extends ActionSupport implements ModelDriven<UserArticle>{
//模型驱动使用的对象
    private UserArticle userArticle = new UserArticle();
    @Override
    public UserArticle getModel() {
        return userArticle;
    }
    private String fileFileName; //上传的文件名称,固定写法:name+FileName;
    private String fileContentType;  //上传的文件类型,固定写法:name+ContentType;

    public String getFileFileName() {
        return fileFileName;
    }
    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }
    public String getFileContentType() {
        return fileContentType;
    }

    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }
public String newspost(){

        try{
            String realPath = ServletActionContext.getServletContext().getRealPath("/file");//取file文件夹的路径
            File dir = new File(realPath);//file文件夹
            if(!dir.exists()){//如果未创建则新建
                dir.mkdirs();
            }

            if(userArticle.getFile()!=null)
            FileUtils.moveFile(userArticle.getFile(),new File(dir,fileFileName));//commons-io.jar包的FileUtils方法,把上传的的文件移到file文件夹
        }
        catch (IOException e) {  
            System.out.println("上传失败");
            e.printStackTrace();
        }  

        userArticle.setFilename(fileFileName);//保存文件名
        return "success";
    }
}

接下来就是文件下载功能:

UserArticleAction.java

public class UserArticleAction extends ActionSupport implements ModelDriven<UserArticle>{
//模型驱动使用的对象
    private UserArticle userArticle = new UserArticle();
    @Override
    public UserArticle getModel() {
        return userArticle;
    }
private String ContentType;
        private InputStream fileStream;

        private String docName;
        public String getDocName() throws UnsupportedEncodingException {

            docName = userArticle.getFilename();
            docName = URLEncoder.encode(docName,"UTF-8");//解决文件名中文显示不出或者乱码问题
            return docName;
        }
        public InputStream getFileStream() throws UnsupportedEncodingException {
            return fileStream;
        }
        public void setFileStream(InputStream fileStream) {
            this.fileStream = fileStream;
        }

        public String download(){
            System.out.println("filename:"+userArticle.getFilename());
            fileStream = ServletActionContext.getServletContext().getResourceAsStream("/file/"+userArticle.getFilename());          
            return "success";
        }
}

struts.xml

        <action name="download" class="userArticleAction" method="download">
            <result name="success" type="stream">
                
                <param name="inputName">fileStreamparam>
                
                <param name="contentDisposition">attachment;filename=${docName}param>
            result>
        action>

你可能感兴趣的:(ssh框架,Struts2)