pwlazy的专栏

与时俱进,开拓创新

深入分析webwork中的文件上传机制
点击上传按钮后,webwork的程序流如下:
step 1)进入ServletDispatcher.service
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException {
       ........

            request = wrapRequest(request);
      .........    
    }

step2)进入ServletDispatcher.wrapRequest
protected HttpServletRequest wrapRequest(HttpServletRequest request) throws IOException {
        ........................

        if (MultiPartRequest.isMultiPart(request)) {
            request = new MultiPartRequestWrapper(request, getSaveDir(), getMaxSize());
        }

        return request;
    }

step3)进入MultiPartRequestWrapper的构造方法

public MultiPartRequestWrapper(HttpServletRequest request, String saveDir, int maxSize) throws IOException {
           .....................
          //step3.1)获取webwork.preperties配置的parser
            String parser = "";

            parser = Configuration.getString("webwork.multipart.parser");

            // If it's not set, use Pell
            if (parser.equals("")) {
                log.warn("Property webwork.multipart.parser not set." +
                        " Using com.opensymphony.webwork.dispatcher.multipart.PellMultiPartRequest");
                parser = "com.opensymphony.webwork.dispatcher.multipart.PellMultiPartRequest";
            }
            // legacy support for old style property values
            else if (parser.equals("pell")) {
                parser = "com.opensymphony.webwork.dispatcher.multipart.PellMultiPartRequest";
            } else if (parser.equals("cos")) {
                parser = "com.opensymphony.webwork.dispatcher.multipart.CosMultiPartRequest";
            } else if (parser.equals("jakarta")) {
                parser = "com.opensymphony.webwork.dispatcher.multipart.JakartaMultiPartRequest";
            }
          
          //step3.2)获取后通过反射实例化parser
            try {
                Class baseClazz = com.opensymphony.webwork.dispatcher.multipart.MultiPartRequest.class;

                Class clazz = Class.forName(parser);

                // make sure it extends MultiPartRequest
                if (!baseClazz.isAssignableFrom(clazz)) {
                    addError("Class '" + parser + "' does not extend MultiPartRequest");

                    return;
                }

                // get the constructor
                Constructor ctor = clazz.getDeclaredConstructor(new Class[]{
                    Class.forName("javax.servlet.http.HttpServletRequest"),
                    java.lang.String.class, int.class
                });

                // build the parameter list
                Object[] parms = new Object[]{
                    request, saveDir, new Integer(maxSize)
                };

                // instantiate it
                multi = (MultiPartRequest) ctor.newInstance(parms);
          .................................................
    }

//step4 进入JakartaMultiPartRequest的构造方法(我在webwork配置的parser是Jakarta所以进入了这个方法,如果你配置不同的parser会进入不同的parser
public JakartaMultiPartRequest(HttpServletRequest servletRequest, String saveDir, int maxSize)
            throws IOException {
     
       //设置保存参数
        DiskFileUpload upload = new DiskFileUpload();
        // we must store all uploads on disk because the ww multipart API is missing streaming
        // capabilities
        upload.setSizeThreshold(0);
        upload.setSizeMax(maxSize);
        if (saveDir != null) {
            upload.setRepositoryPath(saveDir);
        }

        // Parse the request
        try {
          
           //此方法生成文件,将请求中的每个参数都生成一个临时文件比如upload_00000017.tmp,           upload_00000018.tmp等,就算是form提交的参数也如此
            List items = upload.parseRequest(servletRequest);

            ......................
    }
执行完第四步,然后推出ServletDispatcher.wrapRequest,进入serviceAction方法,开始action及其拦截器的栈调用

在此过程中会删除非上传文件的临时文件。

进入action和调用栈后,拦截器或action可通过如下代码访问上传的临时文件
MultiPartRequestWrapper wrapper = (MultiPartRequestWrapper) req;
File doc =  wrapper.getFiles("doc")[0];

从上面的分析可以看出:
1)如果你使用webwork来上传文件(在进入action栈之前不修改源码或者做一些扩展、覆盖之类的动作),在进入action栈的时候文件已经上 传,而且其文件名很难跟踪(upload_00000017.tmp,到底是00000017,0000018,或者0000022等等),毕竟有很多人 上传文件,所以临时文件名很难确定,所以如果你想知道上传的进度很难。
2)利用webwork上传文件是两次拷贝过程,webwork首先从request的输入流中将文件流输出到一个临时文件,然后你再将此临时文件拷贝到你需要指定的路径。这是好是坏?或者实现一个servlet一次搞定不依赖webwork框架,或者改变webwork框架?也许这依赖于具体的需求
阅读更多
想对作者说点什么? 我来说一句

webwork中上传文件

2008年12月13日 28KB 下载

没有更多推荐了,返回首页

不良信息举报

深入分析webwork中的文件上传机制

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭