點擊上傳按鈕後,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的輸入流中將文件流輸出到一個臨時文件,然後你再將此臨時文件拷貝到你需要指定的路徑。