ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Struts2实现文件上传

2019-09-14 09:38:42  阅读:164  来源: 互联网

标签:文件 return String title Struts2 uploadContentType 上传 public


一 视图

1 uploadForm.jsp

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>简单的文件上传</title>
</head>
<body>
<s:form action="upload"
    enctype="multipart/form-data">
    <s:textfield name="title" label="文件标题"/>
    <s:file name="upload" label="选择文件"/>
    <s:submit value="上传"/>
</s:form>
</body>
</html>

2 succ.jsp

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>上传成功</title>
</head>
<body>
    上传成功!<br/>
    文件标题:<s:property value=" + title"/><br/>
    文件为:<img src="<s:property value="'uploadFiles/'
        + uploadFileName"/>"/><br/>
</body>
</html>

二 配置文件

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">    
<struts>
    <!-- 设置该应用使用的字符集 -->
    <constant name="struts.i18n.encoding" value="GBK"/>
    <package name="lee" extends="struts-default">
        <!-- 配置处理文件上传的Action -->
        <action name="upload" class="org.crazyit.app.action.UploadAction">
            <!-- 动态设置Action的属性值 -->
            <param name="savePath">/uploadFiles</param>
            <!-- 配置Struts 2默认的视图页面 -->
            <result>/WEB-INF/content/succ.jsp</result>    
        </action>
        <action name="*">
            <result>/WEB-INF/content/{1}.jsp</result>    
        </action>
    </package>
</struts>

三 action

package org.crazyit.app.action;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import java.io.File;
import java.io.*;


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);
    }

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

    // upload的setter和getter方法
    public void setUpload(File upload)
    {
        this.upload = upload;
    }
    public File getUpload()
    {
        return (this.upload);
    }

    // uploadContentType的setter和getter方法
    public void setUploadContentType(String uploadContentType)
    {
        this.uploadContentType = uploadContentType;
    }
    public String getUploadContentType()
    {
        return (this.uploadContentType);
    }

    // uploadFileName的setter和getter方法
    public void setUploadFileName(String uploadFileName)
    {
        this.uploadFileName = uploadFileName;
    }
    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;
    }
}

四 测试

标签:文件,return,String,title,Struts2,uploadContentType,上传,public
来源: https://blog.csdn.net/chengqiuming/article/details/100799893

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有