前段时间做一个项目,用户提出需要一次性上传多个文件,这个多=X,就是不确定,于是在客户端采用脚本(JAVASCRIPT)的方式来满足了用户的需求。
1。此项目采用的是WEBWORK的框架结构,webwork 文件上传采用拦截器的方式,叫fileUpload 可供选择的上传组件有pell和JAKATA和COS,具体的使用是通过webwork.properties这个文件来配置的,即
### Parser to handle HTTP POST requests, encoded using the MIME-type multipart/form-data
#webwork.multipart.parser=cos
webwork.multipart.parser=pell
在这里我采用了PELL的方式
注意:在使用这三种方式时,都会使用到相应的JAR包,PELL的方式会使用一个LOG4及pell-multpart.jar,初学使用者一定要注意
2。客户端脚本是通过新增一个文件上传的节点来实现的,这个新增的文件上传的HTML节点具有相同的名字,部分代码如下:
<script language="javascript" type="text/javascript"></script>
调用部分的html代码如下:
这样做的目的是能够通过节点的方式让用户选择的上传多个文件
注意:因为上传多个文件到WEBWORK后台时,已经选择了原有的文件名,所以只能是增加一个文件名的节点,并在ACTION里面处理(看官如果有好的见意和想法欢迎指点:)
3。完成了配置和客户端的代码后,就是后台ACTION里面的处理了
1。此项目采用的是WEBWORK的框架结构,webwork 文件上传采用拦截器的方式,叫fileUpload 可供选择的上传组件有pell和JAKATA和COS,具体的使用是通过webwork.properties这个文件来配置的,即
### Parser to handle HTTP POST requests, encoded using the MIME-type multipart/form-data
#webwork.multipart.parser=cos
webwork.multipart.parser=pell
在这里我采用了PELL的方式
注意:在使用这三种方式时,都会使用到相应的JAR包,PELL的方式会使用一个LOG4及pell-multpart.jar,初学使用者一定要注意
2。客户端脚本是通过新增一个文件上传的节点来实现的,这个新增的文件上传的HTML节点具有相同的名字,部分代码如下:
<script language="javascript" type="text/javascript"></script>
js 代码
- function addAddition() { //新增节点
- var strInn = ";
- var strHid = ";
- otherAddition.insertAdjacentHTML("beforeEnd",strInn);
- additionName.insertAdjacentHTML("beforeEnd",strHid);
- }
- function delAddition() { //删除节点
- iIndex = otherAddition.childNodes.length
- nIndex = additionName.childNodes.length
- if(otherAddition.childNodes.length <= 2) return;
- otherAddition.removeChild(otherAddition.children(--iIndex));
- otherAddition.removeChild(otherAddition.children(--iIndex));
- additionName.removeChild(additionName.children(--nIndex));
- }
xml 代码
- <div id="otherAddition"><input type="file" onkeydown="return false" id="fileAdditions" name="fileAdditions" style="width:400"><BR>div>
- <span style="cursor:hand;color:red" onclick="addAddition()">再次添加附件span> <span style="cursor:hand;color:red" onclick="delAddition()">删除附件span>
- <span id="additionName" name="additionName"><input type="hidden" id="fileName" name="fileName">span>
这样做的目的是能够通过节点的方式让用户选择的上传多个文件
注意:因为上传多个文件到WEBWORK后台时,已经选择了原有的文件名,所以只能是增加一个文件名的节点,并在ACTION里面处理(看官如果有好的见意和想法欢迎指点:)
3。完成了配置和客户端的代码后,就是后台ACTION里面的处理了
java 代码
- public String getAddtion() throws Exception {
- if (hasActionErrors() || hasFieldErrors()){
- System.out.println("found errors");
- }else if (fileAddition != null) {
- System.out.println("file uploaded to: " + fileAddition.getAbsolutePath());
- String uploadDir = ServletActionContext.getServletContext().getRealPath("\\upload") + "\\dc\\";
- // write the file to the file specified
- System.out.println("the upload file path->" + uploadDir);
- File dirPath = new File(uploadDir);
- if (!dirPath.exists()) {
- System.out.println("build the dir path!");
- if(!dirPath.mkdirs()) {
- olpError.setErrorName("新建目录出错!");
- olpError.setErrorCode("103");
- return ERROR;
- }
- } else
- System.out.println("the path exists");
- if (fileAddition == null) {
- log.debug("file is null!");
- }
- String filePath = uploadDir + fileAddition.getName();
- File target = new File(filePath);
- System.out.println("filename is :" + fileAddition.getName());
- if(target.exists()){
- target.delete();
- }
- fileAddition.renameTo(target);
- FileInputStream fis = new FileInputStream(target);
- byte[] buffer = new byte[fis.available()];
- fis.read(buffer);
- fis.close();
- //publicInfo.setInfoIname(target.getName());
- buffer = null;
- return target.getName();
- }else{
- if(this.opt.equals("update")) {
- PublicInfo pinfoTmp = (PublicInfo)mngPublicInfo.loadByID(publicInfo.getId());
- return pinfoTmp.getInfoIname();
- }
- }
- return "";
- } //注意,增加private File[] fileAdditions;和private String[] fileName;两个成员变理及基get,set方法在action里面