Struts 檔案上傳(子產品)總結
-、功能要求:
1.基本檔案上傳
2.jsp頁面參數指定檔案上傳到伺服器的路徑
3.jsp頁面參數限制上傳檔案類型
4.在伺服器端儲存檔案到檔案夾,儲存檔案路徑到資料庫
二、開發環境
- 開發語言 -- jdk1.5
- web應用伺服器 -- tomcat5.5
- IDE工具 -- eclipse3.2+myeclipse5.1+Subclipse
- 代碼、文檔管理工具 -- Svn1.4
- 資料庫 -- Oracle9i
三、系統架構
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZwpmLzl3cvwVZulGb5t2cqh2LcRXZu9lbkN3Yfd2bsJ2Xw9CXzV2Zh1WavwFdl5mLuR2cj5yZvxmYtA3Lc9CX6MHc0RHaiojIsJye.jpg)
<1> Web層采用struts
<2> Service層采用spring
<3> 持久層采用hibernate
四、上傳子產品的實作(struts層)---相對獨立。
使用元件:Struts Upload
首頁面 | 子頁面 | |
Jsp | b.jsp | a.jsp |
Action | productActionàproductDAOàproduct | UploadAction/UploadForm |
表單 | 産品編号 名稱 生産廠家 上傳檔案路徑(hidden) | 指定檔案路徑 被限制檔案類型 上傳的檔案 |
調用方式 | 外部菜單 | <iframe src=””></iframe> |
執行按鈕 | 送出、重置、傳回 | 上傳 |
工作邏輯:
點選相關菜單後,跳轉到“産品添加”首頁面。在首頁面中設定“允許上傳檔案的類型”和“上傳到伺服器的路徑”,并在<iframe src=””>的路徑中作為參數傳給子頁面(負責上傳檔案)。在首頁面中上傳子架構内點選“浏覽”按鈕,選擇需上傳的檔案,然後點選“上傳”按鈕進行上傳,在UploadAction中進行上傳處理,取得檔案上傳到伺服器的路徑後,通過
request.setAttribute( " filePath " , filePath + fileName);
将檔案在伺服器上的路徑傳回 a.jsp頁面(子頁面),在a.jsp頁面中通過
parent.document.all.filePath.value = " ${filePath} " ;
将在a.jsp子頁面中取得的由UploadAction傳回的檔案路徑再傳給b.jsp首頁面. 這時首頁面已經得到了子頁面傳回的“上傳檔案在伺服器上的路徑”。将添加表單的其他項(如産品編号、名稱、生産廠家等)填寫完整後(這一步可以先于檔案上傳來做),點選“送出”按鈕,送出整個表單(包括了 檔案路徑 在頁面中是以隐式标簽來做),将表單資訊添加到資料庫。
頁面示例圖:
出現問題與解決:
<1>如何從主架構向子架構傳遞參數(允許上傳檔案的類型,上傳到伺服器的路徑 在首頁面中指定)
// b.jsp
<%
String sub_path = " incoming/zygl/equipment " ;
String up_file_type = " txt,rar,zip,doc " ;
%>
// b.jsp <form action="<%=request.getContextPath()%>/productAction.do" method="post" name="ClientForm">
< tr >< td class = " td " >
<html:hidden name="hidd" styleClass="shuru" property="filePath" value="" />
< html:errors property = " filePath " />
</ td >
</ tr > </form>
// b.jsp
< tr >< td class = " td " >< span class = " font13b " > 産品說明書: </ span >
< iframe
src=" ../../../../commons/a.jsp?subPath=<%=sub_path%>&upFileType=<%=up_file_type%>"
name = " display " width = " 300 " height = " 25 "
marginwidth = " 0 " marginheight = " 0 " scrolling = " no " frameborder = " 0 " >
</ iframe >
</ td >
</ tr >
<2>如何從子架構向主架構傳第參數
// a.jsp
< script type = " text/javascript " >
function pathPush() ... {
//子架構向主架構傳值
if("${filePath}" != "null" && "${filePath}" != "")...{
parent.document.all.filePath.value = "${filePath}";
}
// 一些異常情況提示
if("${errMessage}" != "")...{
alert("${errMessage}");
}
}
</ script >
// a.jsp
< body onLoad = " pathPush(); " >
<%
String path = (String) request.getAttribute( " filePath " );
%>
<%
if (path != null ) ... {
%>
上傳成功!
<%
} else ... {
%>
<html:form action="uploadsAction.do" enctype="multipart/form-data">
<html:hidden property="subPath" value="<%=request.getParameter("subPath")%>" />
<html:hidden property="upFileType" value="<%=request.getParameter("upFileType")%>" />
<html:file property="theFile" />
<html:submit value="上傳" />
</html:form>
<%
}
%>
</ body >
// b.jsp
< tr >< td class = " td " >
< html:hidden name = " hidd " styleClass = " shuru " property = " filePath " value = "" />
< html:errors property = " filePath " />
</ td >
</ tr >
<3>路徑亂碼問題
描述:在a.jsp中取得的filePath值為亂碼,且路徑分隔符“/”也不見了。傳到資料庫中的檔案路徑也是亂碼。
如:E:Apache Software FoundationTomcat 5.5/webapps/mmxtincoming/20010331115151_建立 文本文檔.txt
變為
E:Apache Software FoundationTomcat 5.5webappsmmxtincoming?10331115151_怴寶 暥杮暥瀮.txt
問題原因:路徑分隔符“/”,在傳遞過程中被屏蔽。
解決辦法:增加一個字元串替換函數,将“/”替換為“//”則在傳遞過程中就無誤了。
public static String replace(String strSource, String strFrom, String strTo) ... {
if (strSource == null) ...{
return null;
}
int i = 0;
if ((i = strSource.indexOf(strFrom, i)) >= 0) ...{
char[] cSrc = strSource.toCharArray();
char[] cTo = strTo.toCharArray();
int len = strFrom.length();
StringBuffer buf = new StringBuffer(cSrc.length);
buf.append(cSrc, 0, i).append(cTo);
i += len;
int j = i;
while ((i = strSource.indexOf(strFrom, i)) > 0) ...{
buf.append(cSrc, j, i - j).append(cTo);
i += len;
j = i;
}
buf.append(cSrc, j, cSrc.length - j);
return buf.toString();
}
return strSource;
}
相應部位改為:filePath = replace(filePath,"//", "//"+"//");
//UploadAction.java
//取得上傳的檔案
UpLoadForm theForm = (UpLoadForm) form;
FormFile file = theForm.getTheFile();
//如果上傳檔案為空
if(fileName.equals("")){
request.setAttribute("errMessage", "請選擇要上傳的檔案");
return mapping.findForward("display");
}
//檔案類型限制
if(!isValidFile(fileName,theForm.getUpFileType())){
System.out.println("不允許上傳該類型檔案");
request.setAttribute("errMessage", "不允許上傳該類型檔案");
return mapping.findForward("display");
}
//檔案大小限制
if(file.getFileSize() > (10*1024*1024)){
request.setAttribute("errMessage", "檔案大小最大為10M");
return mapping.findForward("display");
}
…… … 中略 … ……
//取檔案儲存到伺服器的路徑
String filePath = request.getRealPath("/") + theForm.getSubPath() + "//";
filePath = replace(filePath,"//", "//"+"//");
//上傳檔案類型設定
private boolean isValidFile(String fileName, String upFileType) {
String[] validFiles;
validFiles = upFileType.split(",");
boolean ret = false;
for (int i = 0; i < validFiles.length; i++) {
if (fileName.toLowerCase().endsWith(validFiles[i])) {
ret = true;
break;
}
}
return ret;
}
五、部分源檔案下載下傳(部分檔案名與例子有出入)
http://fileblog.hjbbs.com/upload/200804/20080401100006614_817_2213.rar
相關論壇文章參考:
http://topic.csdn.net/u/20080331/12/d660792e-1a05-41c9-89e9-272d5d152a2b.html
http://topic.csdn.net/u/20080331/12/d660792e-1a05-41c9-89e9-272d5d152a2b.html