WEB开发网
开发学院WEB开发Jsp 深入分析java中webwork中的文件上传机制 阅读

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

 2008-01-05 18:38:10 来源:WEB开发网   
核心提示:点击上传按钮后,webwork的程序流如下:step 1)进入ServletDispatcher.servicepublic void service(HttpServletRequest request, HttpServletResponse response) throws ServletException {.

点击上传按钮后,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及其拦截器的栈调用

在此过程中会删除非上传文件的临时文件,至于哪一步删除,我还没看出来,有时候很早有时候很晚,有时候甚至没有删除,我怀疑有个dameon在做这个事。

进入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的输入流中将文件流输出到一个临时文件,然后你再将此临时文件拷贝到你需要指定的路径。这是好是坏?


Tags:深入 分析 java

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接