- 浏览: 624726 次
- 性别:
- 来自: 广州
文章分类
最新评论
本文为原创,如需转载,请注明作者和出处,谢谢!
上一篇:Struts1.x系列教程(7):Logic标签库
Struts2教程7:上传任意多个文件
在Web程序中的上传文件的功能一般会使用<input type=file.../>标签在客户端显示输入要上传的文件名的用户接口。如果读者使用过JSP/Servlet来处理客户端上传的文件就会知道,实现这个功能并不是很容易。而在Struts中为我们提供了一种机制,可以非常方便地将客户端上传的文件保存在服务端。
在Struts
Html标签库中提供了一个<html:file>标签,可以和FormFile类型的属性一起使用。通过这个属性返回的FormFile对象,就可以很方便地获得上传文件的InputStream对象,并做进一步地处理。
在Struts中,一个FormFile对象代表一个上传的文件。FormFile实际上是一个接口,我们可以在org.apache.struts.upload包中找到这个接口。FormFile接口有如下五个常用的方法:
1.getInputStream()方法
这个方法用于获得上传文件的java.io.InputStream对象,我们可以从这个InputStream对象中读取上传文件的数据,并将其写到服务端指定的路径下。
2.getFileData()方法
这个方法用于将上传文件的整个内容放到一个byte类型的数组中,如果上传文件的尺寸比较小的话,使用这个方法可以很方便地将上传文件保存到在服务端的指定路径中。
3.getFileName()方法
这个方法用于获得上传文件的文件名(不包括文件在客户机的路径部分)。
4. getFileSize()方法
这个方法用于获得上传文件的字节数。
5. destroy()方法
用于销毁所有和当前上传文件相关的资源。
【第1步】建立上传文件的JSP页面
在<samples工程目录>目录中建立一个uploadFile.jsp文件,代码如下:
<%@tagliburi="http://struts.apache.org/tags-html"prefix="html"%>
<html>
<head>
<title>上传单个文件(文件大小不能超过2M)</title>
</head>
<body>
<%--在<html:form>标签中必须加enctype="multipart/form-data"--%>
<html:formenctype="multipart/form-data"action="uploadFile">
<html:fileproperty="myFile"/><p><%--使用<html:file>标签让用户输入上传文件名--%>
<html:submitvalue="上传"/>
</html:form>
</body>
</html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="ProgId" content="Word.Document"> <meta name="Generator" content="Microsoft Word 11"> <meta name="Originator" content="Microsoft Word 11"> <link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"> <!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style> <!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} </style> <![endif]-->【第2步】建立ActionForm的子类
在<samples工程目录>"src"actionform目录中建立一个UploadForm.java文件,代码如下:
importorg.apache.struts.action.*;
importorg.apache.struts.upload.FormFile;
publicclassUploadFormextendsActionForm
{
privateFormFilemyFile;//这个myFile代表要上传的文件
publicFormFilegetMyFile()
{
returnmyFile;
}
publicvoidsetMyFile(FormFilemyFile)
{
this.myFile=myFile;
}
}
【第3步】建立Struts动作类(Action的子类)
在Struts中,一般在Struts的动作类中处理上传的文件。在<samples工程目录>"src"action目录中建立一个UploadAction.java文件,代码如下:
importjavax.servlet.http.*;
importorg.apache.struts.action.*;
importorg.apache.struts.upload.FormFile;
importjava.io.*;
importactionform.*;
publicclassUploadActionextendsAction
{
protectedvoidsaveFile(FormFileformFile)throwsException
{
//从web.xml文件中获得指定的上传路径
Stringpath=this.getServlet().getServletConfig().getInitParameter("uploadPath");
InputStreamin=formFile.getInputStream();//获得上传文件的InputStream
//在服务端指定的上传路径中建立一个空的文件(文件名为getFileName()方法返回的值)
FileOutputStreamfout=newFileOutputStream(path+formFile.getFileName());
bytebuffer[]=newbyte[8192];
intcount=0;
//开始向上传路径中刚建立的文件写入数据,每次写8k字节
while((count=in.read(buffer))>0)
{
fout.write(buffer,0,count);
}
fout.close();
formFile.destroy();//上传成功后,销毁当前上传文件的资源
}
publicActionForwardexecute(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse)
{
UploadFormuForm=(UploadForm)form;
PrintWriterout=null;
try
{
response.setCharacterEncoding("GBK");
out=response.getWriter();
saveFile(uForm.getMyFile());//将上传文件保存到指写的路径(在web.xml中配置)
out.println("上传文件成功.");
}
catch(Exceptione)
{
out.println(e.getMessage());
}
returnnull;
}
}
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="ProgId" content="Word.Document"> <meta name="Generator" content="Microsoft Word 11"> <meta name="Originator" content="Microsoft Word 11"> <link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"> <!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style> <!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} </style> <![endif]--> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="ProgId" content="Word.Document"> <meta name="Generator" content="Microsoft Word 11"> <meta name="Originator" content="Microsoft Word 11"> <link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"> <!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style> <!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} </style> <![endif]--> 在saveFile方法中,首先从web.xml的Servlet初始化参数中获得一个用于保存上传文件的路径。然后从FormFile对象中获得上传文件的InputStream对象,并对这个InputStream对象中的字节进行循环读取,并写到新的文件中。最后,在上传成功后,将FormFile对象销毁。
【第4步】配置struts-config.xml
在这一步来配置一下在第2步和第3步分别建立的ActionForm和Action的子类。打开struts-config.xml文件,在<form-beans>中加入如下的子标签:
在<action-mappings>中加入如下的子标签:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="ProgId" content="Word.Document"> <meta name="Generator" content="Microsoft Word 11"> <meta name="Originator" content="Microsoft Word 11"> <link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"> <!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style> <!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} </style> <![endif]-->【第5步】设置用于保存上传文件的路径
打开web.xml文件,找到一个叫action的Servlet(也就是用于处理Struts动作的Servlet),并在<servlet>中加入如下的子标签(假设保存上传文件的路径为D:"upload",路径的最后一个字符必须是“"”):
<param-name>uploadPath</param-name>
<param-value>D:\upload\</param-value>
</init-param>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="ProgId" content="Word.Document"> <meta name="Generator" content="Microsoft Word 11"> <meta name="Originator" content="Microsoft Word 11"> <link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"> <!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style> <!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} </style> <![endif]-->
【第6步】限制上传文件的大小
这一步是可选的,如果不限制上传文件的大小,就意味着可以上传任意大小的文件。而一般的应用程序,如电子相册,网络硬盘都会限制上传文件的最大尺寸。
打开struts-config.xml文件,在<struts-config>中加入如下的子标签:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="ProgId" content="Word.Document"> <meta name="Generator" content="Microsoft Word 11"> <meta name="Originator" content="Microsoft Word 11"> <link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"> <!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><!--[if !mso]> <style> st1":*{behavior:url(#ieooui) } </style> <![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style> <!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} </style> <![endif]--> 上面的<controller>标签将上传文件的最大尺寸设为2M,maxFileSize属性值的单位可以是M,也可以是K或G,如2K,5G等。
启动Tomcat后,在IE中输入如下的URL来测试程序:
http://localhost:8080/samples/uploadFile.jsp
下一篇:Struts1.x系列教程(9):上传任意多个文件
国内最棒的Google Android技术社区(eoeandroid),欢迎访问!
《银河系列原创教程》发布
《Java Web开发速学宝典》出版,欢迎定购
相关推荐
struts1.x入门教程
适用于Struts1.x初学者(网摘共25节)
Struts1.x常用的开发包,需要学习struts1.x朋友可以下载
struts1.x多文件上传, 感觉不错.
struts1.x和struts2.x区别
Struts1.x的上传文件示例。Struts1.x的教程参考我的Blog:http://blog.csdn.net/boyazuo
Struts1.x的登录示例,Struts1.x相关详细教程参考我的Blog:http://blog.csdn.net/boyazuo
freemarker与struts 1.x 结合,生成html 在http://blog.csdn.net/chen1255 的web中有详细描述关于freemarker与struts1.x的结合
Struts 2.x权威指南(第3版)
在这里,通过一个小例子稍微总结了下struts1和struts2的区别,希望这个能帮助到初学struts的朋友
java Struts 1.x 框架 Web 开发 java Struts 1.x 框架 Web 开发
文章来自:http://www.blogjava.net/nokiaguy/
Struts1.x系列教程(1)文件乱码问题的解决.doc )
Struts1.x入门与提高系列教程.pdf 很好用的,拿来与大家分享
struts安装,配置 如果你已经了解了MVC,那么使用Struts进行开发对你来说将是一件很爽的事情。下面来说一下基本配置方法(Struts1.X):1.下载Struts:2.加载jar包:3.配置web.xml:4.配置Struts-config.xml文件:
struts1.x apps 所有最新的Struts1.x项目应用 struts1.x apps 所有最新的Struts1.x项目应用 欢迎大家下载。。。
IBMStruts2培训PPT (大全集) 主讲:刘雷 完整实用 第一章:认识体验Struts2.ppt 第二章:struts2架构剖析.ppt ...第八章:struts2文件上传下载.ppt 第九章:struts2拦截器.ppt + Struts2开发详解.ppt
struts1 国际化 源代码 struts1 国际化 工程
struts2.x所有包及配置文件