struts2――文件上传

upload.jsp

<%@page pageEncoding="utf-8"%>
<html>
    <head></head>
    <body>
        <h1>上传文件示例</h1>
        <form action="upload.action" enctype="multipart/form-data" method="post">
            <input type="file" name="some"/><br/>
            <input type="submit" value="提交"/>
        </form>
    </body>
</html>

struts.xml:

<!-- 这是一个文件上传到小例子 -->
<struts>
    <!-- 改变Struts2上传文件的最大限制 -->
    <constant name="struts.multipart.maxSize" value="5000000"></constant>
    <package name="demo" namespace="/demo" extends="struts-default">
        <!-- 转到上传页面的Action -->
        <action name="toUpload">
            <result name="success">/WEB-INF/upload.jsp</result>
        </action>
        <!-- 上传文件Action -->
        <action name="upload" class="com.tarena.action.UploadAction">
            <interceptor-ref name="fileUpload">
                <!-- 改变Action上传文件的限制 -->
                <param name="maximumSize">2000000</param>
            </interceptor-ref>
            <interceptor-ref name="basicStack"></interceptor-ref>
            <result name="success">/WEB-INF/ok.jsp</result>
            <result name="error">/WEB-INF/error.jsp</result>
        </action>
    </package>
</struts>

Action:

public class UploadAction implements ServletContextAware {
    // 输入属性
    private File some;
    private String someFileName;
    private ServletContext ctx;
    public String getSomeFileName() {
        return someFileName;
    }
    public void setSomeFileName(String someFileName) {
        this.someFileName = someFileName;
    }
    public File getSome() {
        return some;
    }
    public void setSome(File some) {
        this.some = some;
    }
    public String execute() {
        System.out.println("SOME" + some);
        System.out.println("FileName:" + someFileName);
        if (some == null) {
            return "error";
        }
        // 1、根据输入属性,构建存储的路径
                           
        // 指定上传文件名
        String name = "upload_" + someFileName;
        // 构建一个相对路径
        String path = "upload/" + name;
        // 根据相对路径,去构建绝对路径
        path = ctx.getRealPath(path);
        System.out.println("PATH:" + path);
        // 2、根据存储的路径,将输入属性复制到路径下
        FileUtil.copy(some, new File(path));
        // 3、返回特征字符串,找Result
        return "success";
    }
    public void setServletContext(ServletContext sc) {
        ctx = sc;
    }
}

FileUtil.java:提供文件复制

public class FileUtil {
    public static boolean copy(File src, File dest) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(src));
            bos = new BufferedOutputStream(new FileOutputStream(dest));
            byte[] bts = new byte[1024];
            int len = -1;
            while ((len = bis.read(bts)) != -1) {
                bos.write(bts, 0, len);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


可以参考http://www.blogjava.net/max/archive/2007/03/21/105124.html


你可能感兴趣的:(struts2,上传文件,upload)