struts 文件上传(主框架、子框架传值等问题)
2008-04-01 21:36
381 查看
Struts 文件上传(模块)总结
-、功能要求:
1.基本文件上传
2.jsp页面参数指定文件上传到服务器的路径
3.jsp页面参数限制上传文件类型
4.在服务器端保存文件到文件夹,保存文件路径到数据库
二、开发环境
开发语言 -- jdk1.5
web应用服务器 -- tomcat5.5
IDE工具 -- eclipse3.2+myeclipse5.1+Subclipse
代码、文档管理工具 -- Svn1.4
数据库 -- Oracle9i
三、系统架构
<1> Web层采用struts
<2> Service层采用spring
<3> 持久层采用hibernate
四、上传模块的实现(struts层)---相对独立。
使用组件:Struts Upload
工作逻辑:
点击相关菜单后,跳转到“产品添加”主页面。在主页面中设置“允许上传文件的类型”和“上传到服务器的路径”,并在<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
问题原因:路径分隔符“/”,在传递过程中被屏蔽。
解决办法:增加一个字符串替换函数,将“/”替换为“//”则在传递过程中就无误了。
/**//* 替换函数
* strSource 需要替换的源字符串
* strFrom 被替换的字符串
* strTo 替换为的字符串
*/
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
-、功能要求:
1.基本文件上传
2.jsp页面参数指定文件上传到服务器的路径
3.jsp页面参数限制上传文件类型
4.在服务器端保存文件到文件夹,保存文件路径到数据库
二、开发环境
开发语言 -- jdk1.5
web应用服务器 -- tomcat5.5
IDE工具 -- eclipse3.2+myeclipse5.1+Subclipse
代码、文档管理工具 -- Svn1.4
数据库 -- Oracle9i
三、系统架构
<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
问题原因:路径分隔符“/”,在传递过程中被屏蔽。
解决办法:增加一个字符串替换函数,将“/”替换为“//”则在传递过程中就无误了。
/**//* 替换函数
* strSource 需要替换的源字符串
* strFrom 被替换的字符串
* strTo 替换为的字符串
*/
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
相关文章推荐
- 关于SSH框架文件上传到服务器自动删除问题的解决方法。
- struts文件上传以及中文乱码问题
- Spring框架文件上传的问题
- struts中post方式上传文件,enctype="multipart/form-data",request.getParameter("file") 是null的问题分析
- 转:关于我使用spring mvc框架做文件上传时遇到的问题
- struts文件上传——文件过大时错误提示的配置问题说明
- 解决DWZ框架文件上传没有关闭当前的dialog的问题
- CI框架下,上传中文文件和发送中文邮件的标题乱码问题解决
- struts文件上传和遇到的问题
- 解决ext+struts文件上传,json返回结果无法读取到的问题
- Struts1框架四之文件上传
- struts 上传文件乱码问题解决方法
- Java三大框架之struts的多文件上传
- struts 多文件上传--多个文件的name问题
- Struts 1.3.10 上传文件时发生问题。1.3.8,1.2.9没有类似问题
- struts上传文件问题
- Struts文件上传allowedTypes问题,烦人的“允许上传的文件类型”
- Struts1.2文件上传注意的问题
- Struts文件上传allowedTypes问题,烦人的“允许上传的文件类型”