现在的位置: 首页 > 综合 > 正文

Struts2文件上传问题

2018年01月09日 ⁄ 综合 ⁄ 共 2394字 ⁄ 字号 评论关闭

Struts2文件上传问题

该Action作为接收参数以及上传逻辑

public class UploadAction extends ActionSupport
{
    //封装文件标题请求参数的属性
    private String title;
    //封装上传文件域的属性
    private File upload;
    //封装上传文件类型的属性
    private String uploadContentType;
    //封装上传文件名的属性
    private String uploadFileName;
    //直接在struts.xml文件中配置的属性
    private String savePath;
    //接受struts.xml文件配置值的方法
    public void setSavePath(String value)
    {
        this.savePath = value;
    }
    //返回上传文件的保存位置
    private String getSavePath() throws Exception 
    {
        return ServletActionContext.getServletContext()
            .getRealPath(savePath);
    }

    //文件标题的setter和getter方法
    public void setTitle(String title) 
    {
        this.title = title; 
    }
    public String getTitle()
    {
        return (this.title); 
    }

    //上传文件对应文件内容的setter和getter方法
    public void setUpload(File upload) 
    {
        this.upload = upload; 
    }
    public File getUpload() 
    {
        return (this.upload); 
    }

    //上传文件的文件类型的setter和getter方法
    public void setUploadContentType(String uploadContentType) 
    {
        this.uploadContentType = uploadContentType; 
    }
    public String getUploadContentType()
    {
        return (this.uploadContentType); 
    }

    //上传文件的文件名的setter和getter方法
    public void setUploadFileName(String filename) 
    {
        // 防止图片重名,将图片改名
        String format = filename.substring(filename.lastIndexOf("."));
        String file_name = filename.substring(filename.lastIndexOf("\\") + 1,
                filename.lastIndexOf("."));
        Date m_date_now = new Date();
        SimpleDateFormat timeformat = new SimpleDateFormat("yyyyMMddHHmmss",
                java.util.Locale.CHINA);
        String m_date = timeformat.format(m_date_now);
        filename = file_name + m_date + format;
        this.uploadFileName = filename; 
    }
    public String getUploadFileName() 
    {
        return (this.uploadFileName); 
    }
    
    @Override
    public String execute() throws Exception
    {
        //以服务器的文件保存地址和原文件名建立上传文件输出流
        FileOutputStream fos = new FileOutputStream(getSavePath()
            + "\\" + getUploadFileName());
        FileInputStream fis = new FileInputStream(getUpload());
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = fis.read(buffer)) > 0)
        {
            fos.write(buffer , 0 , len);
        }
        fos.close();
        //在这添加写进数据库的语句
        return SUCCESS;
    }
}
 

上传界面

<s:form action="uploadPro"
    enctype="multipart/form-data">
    <s:textfield name="title" label="文件标题"/><br />
    <s:file name="upload" label="选择文件"/><br />
    <s:submit value="上传"/>
</s:form>

注意该界面中属性的名字如title,upload要与Action中相同

显示界面

上传成功!<br/>
    文件标题:<s:property value=" + title"/><br/>
    文件为:<img src="<s:property value="'upload/' 
        + uploadFileName"/>"/><br/>

注意要在webRoot文件夹下创建文件夹upload,否则会爆FileNotFoundException

Struts配置文件

<!-- 配置处理文件上传的Action -->
        <action name="uploadPro" class="*.UploadAction">
            <!-- 动态设置Action的属性值 -->
            <param name="savePath">/upload</param>
            <!-- 配置Struts 2默认的视图页面 -->
            <result>succ.jsp</result>    
        </action>
        <action name="*">
            <result>{1}.jsp</result>    
        </action>

最后要将Struts2的7个包导入就好了。

抱歉!评论已关闭.