Struts2拦截器的应用:文件上传

1、单文件上传

1.1 处理文件上传的Action类

public class UpLoadAction extends ActionSupport{
    private File upload;                  //封装上传文件 xxx
    private String uploadContentType;     //封装上传文件类型xxxContentType固定写法
    private String uploadFileName;        //封装上传文件名xxxFileName固定写法
    private String savePath;              //封装上传文件在服务器tomcat容器上的保存路径
    private FileInputStream fileInput;
    private FileOutputStream fileOutput;

    public File getUpload() {
        return upload;
    }
    public void setUpload(File upload) {
        this.upload = upload;
    }
    public String getUploadContentType() {
        return uploadContentType;
    }
    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }
    public String getUploadFileName() {
        return uploadFileName;
    }
    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public String getSavePath(){
        //以下ServletActionContext.getServletContext().getRealPath(“”)方法获取本工程在tomcat容器中的真实绝对路径,加上参数savepath,进一步得到下一级savepath的绝对路径
        String realpath = ServletActionContext.getServletContext().getRealPath(savePath);
        return realpath;
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }

    public String execute() throws Exception{
        upload.getAbsolutePath();
        fileInput = new FileInputStream(getUpload());     //创建文件输入流
        fileOutput = new FileOutputStream(getSavePath() + "\\" + getUploadFileName());  //创建文件输出流
        byte[] b = new  byte[2048];    //缓冲数组大小2KB
        int len = 0;
        while((len=fileInput.read(b))>0)    //finout.read(b)实现从输入流读2KB数据到数组b中,返回读取的字节数len,遇到文件尾返回-1
        {
            fileOutput.write(b,0,len);     //将b中len字节数据写到输出流中
        }
        fileInput.close();              //关闭输入流
        fileOutput.close();              //关闭输出流
        return "success";
    }
}

1.2 JSP上传页面

<s:form action="upload"  enctype="multipart/form-data">
     选择文件:<s:file name="upload" ></s:file>
     <s:submit  value="上传"></s:submit>
</s:form>
//其他页面(略)

1.3 struts配置

//struts.xml
<struts>
    <constant name="struts.devMode" value="false" />
     <include file="struts-default.xml" />  

    <package name="up-load" extends="struts-default">
        <action name="upload" class="com.action.UpLoadAction">
            <param name="savePath">/uploadAdd</param>  <!-- 部署本工程前,要在WebRoot下手工创建文件夹uploadAdd,保存的文件 -->
            <result name="success">upsuccess.jsp</result>
            <result name="input">uperror.jsp</result><!--拦截不通过自动返回"input"视图-->
            <interceptor-ref name="fileUpload">
                <param name="allowedTypes">
                    image/gif,image/jpeg,image/x-png,image/bmp
                </param>   <!-- 允许上传的文件类型 -->
                <param name="maximumSize">2048000</param>   <!-- 限制上传文件大小为2048000字节  --> 
            </interceptor-ref>
            <interceptor-ref name="defaultStack"/> <!--在内置拦截器后面引用 -->           
        </action>

    </package>
</struts>

2、多文件上传

2.1 处理文件上传的action类

public class UploadAction extends ActionSupport {
    private List<File> files;//xxx
    private List<String> filesFileName;//xxxFileName
    private List<String> filesContentType;//xxxContentType
    private String savedPath;

    public List<File> getFiles() {
        return files;
    }
    public void setFiles(List<File> files) {
        this.files = files;
    }
    public List<String> getFilesFileName() {
        return filesFileName;
    }
    public void setFilesFileName(List<String> filesFileName) {
        this.filesFileName = filesFileName;
    }
    public List<String> getFilesContentType() {
        return filesContentType;
    }
    public void setFilesContentType(List<String> filesContentType) {
        this.filesContentType = filesContentType;
    }

    public String getSavedPath() {
        String realPath=ServletActionContext.getServletContext().getRealPath(savedPath);
        return realPath;
    }
    public void setSavedPath(String savedPath) {
        this.savedPath = savedPath;
    }
    public String execute(){
        try{
            for(int i=0;i<files.size();i++){
                OutputStream out=new FileOutputStream(new File(this.getSavedPath(),filesFileName.get(i)));

                InputStream in=new FileInputStream(files.get(i));
                byte [] buff=new byte[1024];
                int len=0;
                while(-1!=(len=in.read(buff))){
                    out.write(buff,0,len);
                }
                in.close();
                out.close();
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return "success";
    }
}

2.2 JSP上传页面

<h3>多文件上传</h3>
<s:form action="upload" enctype="multipart/form-data">
    <s:file name="files"></s:file>
    <s:file name="files"></s:file>
    <s:submit value="上传"></s:submit>
</s:form>

2.3 struts配置

//struts.xml
<struts>    
    <constant name="struts.devMode" value="false" />
    <include file="struts-default.xml"/>

    <package name="File" extends="struts-default">
        <action name="upload" class="com.action.UploadAction">
            <param name="savedPath">/uploadedfiles</param>
            <result name="success">upload_success.jsp</result>

            <interceptor-ref name="fileUpload">
                <param name="allowedTypes">image/gif,image/jpeg,image/x-png,image/bmp</param>
                <param name="maximumSize">10240000</param>
            </interceptor-ref>
            <interceptor-ref name="defaultStack"/>
        </action>
    </package>
</struts>    

2.4 上传成功页面

//显示上传的图片
<h3>上传成功</h3>
上传的文件:<br>
<s:iterator value="filesFileName" id="filename">
    <s:property value="#filename"/>
    <img src="uploadedfiles/<s:property value='#filename'/>" width="150" height="70"/>
    <br>
</s:iterator>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值