开发者学堂课程【Java Web开发系列课程 - Struts2框架入门:批量文件上传】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/537/detail/7321
批量文件上传
目录:
一、批量文件上传介绍
二、empty()介绍
三、remove([expr])介绍
四、Server.xml 代码演示
五、Action 处理代码
六、Struts.xml 配置文件
一、批量文件上传介绍
通常情况下,在上传图片方面,需要多张图片上传,首先与单个文件上传不一样的是,在页面上会发生改变。struts2可以将批量文件处理为数组上传到 action 中 。
Jsp 代码:
<style type="text/css">
p{margin:5px;}
</style>
<script type="text/javascript src="js/jquery-1.11.3.js"></script>
<script type="text/javascript">$(function(){
$("#btn').click(function(){
var field "<p><input typem'file"
namem'file' /><input typem"button’ valuem"删除" onclick='removed(this);'/></p>";
$("#files').append(field);
});
});
function removed(o){
$(o).parent().remove();
}
</script>
</head>
</script>
</head>
<body>
<form action="batch.action" method="post"
enctype="multipart/form-data">
文件:<input type="file" namea"file"/><input
type="button" id="btn"value="添加"/>
<div id="files"></div>
<input type="submit" value="上传"/>
</form>
</body>
二、empty() 介绍
返回值 jQuery
概述 V1.0
删除匹配的元素集台中所有的子节点
示例
描述:
把所有段落的子元素(包括文本节点)删除
HTML 代码:
<p>Hello,<span)Persen</span><a href="#">and person</a></p >
jQuery 代码:
$("p"). enpty():
结果:
<p></p>
三、remove([expr])介绍
概述
从 DOM 中删除所有匹配的元素
这个方法不会把匹配的元素从jQuery对象中删除,因而可以在将来再使用这些匹配的元素,但除了这个元素本身得以保留之其他的比如绑定的事件,附加的数据等都会被移除。
参数
expr String V1.0
用于筛选元素的 jQuery 表达式
示例描述:
从 DOM 中把所有段落删除
HTML 代码:
<p>Hello</p > how are <p>you?</p >
jQuery 代码:
$(“p").remove ();
结果:
how are
描述:
从 DOM 中把带有 hello 类的段落删除
HTML 代码:
<p class= "hello">Hello</p> how are <p>you?</p>
四、Server.xml 代码演示
<!-- Use the tockoutreaim to pievens attempes to guess user pesswords
Via a bruse-torce attack -->
Reaim clasalame="ora.spacbe gatalina.realm.lockoutResln',
< !-- This Realm uses the UserDatabase configured in the glebal JNDI
resouress under the key "userDatabase", Any edies
that are performed against this userDatabase art inmediately available for use by the Realm.-->
<Reaim classHame="org.apache.catalino.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name-"locathost" appBase=”webapps"
unpackWARs=”true” autoDeplove="true",
<!-- Singhesignon vaive, share authentication between web applications
Documentation ati dock/confia/valve.htmi -->
<!--
<Valve classname="org.apache.catalina.authenticator
SinglesignOn"/>
-->
<!-- Aecess leg procnssns all example.
Documentation as: /docs/contia/valve.html
Note: The pattern used is equivalens to wsing pattern-"ccrmon" --
<Valve classNane="org.pache.cataling.valves,AccessLocValve" directory="logs”
Prefix="localhost_ access_ log." suttix=".txt"
Pattern="&h &'1 &o &t dootiorggoot! &s *b" />
<Context path="/filcupload" doBase="E:(resooces1150526\struts2\demo 14struts_2 fileupload\WehRoot" reloadablemstrue
</Hoat>
</Engine>
</Service>
</Server>
五、Action 处理代码
public class BatchUploadAction
private File[] file;
private String[] fileFileName;
private String[] fileContentType;
public String execute() throws I0Exception{
//写文件的过程
HttpServletRequest reguest =
ServletActionContext.getRequest();
String path=request.getRealPath("/upload");
for(int i=0:i<file.length;i++){
InputStream is = new FileInputStream(file[i]);
Outputstream os = new FileOutputstream(new
File(path,fileFileName[i]));
byte[] buffer = new byte[200]; int len=0;
while((len=is.read(buffer))!=-1){
os.write(buffer,8,len);
os.close(); is.close();
return Action.SUCCESS;
}
public File[] getFile(){
return file;
}
public void setFile(File[] file){
this.file = file;
}
public String[] getFileFileName(){
return fileFileName;
}
public void setFileFileName(String[] fileFileName){
this.fileFileName = fileFileName;
}
public String[] getFileContentType(){
return fileContentType;
}
public void setFileContentType(String[] fileContentType){
this.fileContentType = fileContentType;
}
}
六、Struts.xml 配置文件
<action name="batch" class="cn.sxt.action.BatchUploadAction"
<result>/index.jsp</result>
<interceptor-ref name="fileUpload"
<param name="maximumSize">20971520</param)
</interceptor-ref>
<interceptor-ref namem"defoultstack"/>
</action>