java

位置:IT落伍者 >> java >> 浏览文章

Struts轻松搞定多文件上传


发布日期:2020年08月01日
 
Struts轻松搞定多文件上传

在前面的《Apache Commons FileUpload实现多文件上传》文章中介绍了使用第三方组件ApacheCommons FileUpload来实现多文件的上传对比今天用struts实现的多文件上传总觉得还是struts用起来更加顺手也更加简洁方便不忍自己独享所以拿出来跟大家一块分享一下如果你读过前面提到的那篇文章相信再理解本文将会更加容易另外提供源码以方便大家学习由于本文属于一片纯技术文章废话就不多说了下面看一下struts是如何实现多文件上传的吧

首先来看前台在前台页面中通过js实现上传控件的添加和删除描述的详细一些就是点一下增加一行按钮就会增加一个上传控件同样点击该控件后边的删除则会将该上传控件去除目的在于用js实现添加任意数目的文件的功能

[html]

<span ><%@ page language=java pageEncoding=GB%>

<%@ taglib uri=/tagshtml prefix=html %>

<!DOCTYPE HTML PUBLIC //WC//DTD HTML Transitional//EN>

<html>

<head>

<title>multiUploadDemo</title>

<script type=text/javascript src=js/myjsjs>

</script>

</head>

<body bgcolor=#ffffff>

<div id=status></div>

<form method=post action=uploaddo enctype=multipart/formdata >

<table id=tb></table>

<input type=button name=AddOnLine value=增加一行 onclick=additem(tb)/>

<input type=submit name=btnUpload value=上传 onclick=upload()/>

</form>

</body>

</html>

</span>

在前台页面中引用的js文件中的代码如下

[javascript]

<span > var num = ;

function upload(){

documentgetElementById(status)innerHTML = 文件上传中;

}

function additem(id)

{

var rowcellstr;

row = eval(documentall[++id++])insertRow();

if(row != null )

{

cell = rowinsertCell();

str=<input type=++file++ name=uploadFile[+ num +]file><input type=++button++ value=++删除++ onclick=deleteitem(this++tb++);>

cellinnerHTML=str;

}

num++;

}

function deleteitem(objid)

{

var rowNumcurRow;

curRow = objparentNodeparentNode;

rowNum = eval(documentall+id)rowslength ;

eval(documentall[++id++])deleteRow(curRowrowIndex);

}

function callback(msg)

{

documentgetElementById(status)innerHTML = 文件上传完成<br> + msg;

}</span>

前台页面在实现了动态添加上传控件的功能后真正用来上传文件的后台代码就要隆重登场了后台代码中新建三个类他们分别为UploadActionFormjavaUploadFilejava以及UploadTestActionjava

UploadActionFormjava类代码如下

[java]

<span >package myupload;

import javautilArrayList;

import javautilList;

import orgapachestrutsactionActionForm;

import orgapachestrutsuploadFormFile;

public class UploadActionForm extends ActionForm {

private List myFiles;

public UploadActionForm() {

myFiles = new ArrayList();

}

public List getMyFiles() {

return myFiles;

}

// 注意这个方法的定义 不加中间的循环是会出错的

public UploadFile getUploadFile(int index) {

int size = myFilessize();

if (index > size ) {

for (int i = ; i < index size + ; i++) {

myFilesadd(new UploadFile());

}

}

return (UploadFile) myFilesget(index);

}

public void setMyFiles(List myFiles) {

thismyFiles = myFiles;

}

}

</span>

UploadFilejava类代码如下

[java]

<span >package myupload;

import javaioSerializable;

import orgapachestrutsuploadFormFile;

public class UploadFile implements Serializable {

private FormFile file;

public FormFile getFile() {

Systemoutprintln(run in uploadFilegetFile());

return file;

}

public void setFile(FormFile file) {

thisfile = file;

}

}

</span>

UploadTestActionjava类代码如下

[java]

<span >package myupload;

import javaioFileOutputStream;

import javaioIOException;

import javaioPrintWriter;

import javautilArrayList;

import javaxservletServletException;

import javaxservlethttpHttpServletRequest;

import javaxservlethttpHttpServletResponse;

import orgapachestrutsactionAction;

import orgapachestrutsactionActionForm;

import orgapachestrutsactionActionForward;

import orgapachestrutsactionActionMapping;

import orgapachestrutsuploadFormFile;

import llectionsList;

public class UploadTestAction extends Action {

public ActionForward execute(ActionMapping mapping ActionForm form

HttpServletRequest request HttpServletResponse response)

throws IOException ServletException {

UploadActionForm multiUploadForm = (UploadActionForm) form;

ArrayList myFiles = (ArrayList)multiUploadFormgetMyFiles();

String fileStr = ;

Systemoutprintln(myFilessize());

for (int i = ; i < myFilessize(); i++) {

UploadFile uploadFile = (UploadFile) myFilesget(i);

FormFile file = uploadFilegetFile();

if (file == null) {

Systemoutprintln(file is null);

} else {

FileOutputStream fos = new FileOutputStream(H:\\ + filegetFileName());

foswrite(filegetFileData());

fosflush();

fosclose();

}

}

return mappingfindForward(success);

}

}</span>

最后就是我们的strutsconfigxml文件了

[html]

<span ><?xml version= encoding=ISO ?>

<!DOCTYPE strutsconfig PUBLIC

//Apache Software Foundation//DTD Struts Configuration //EN

/struts/dtds/strutsconfig__dtd>

<strutsconfig>

<formbeans>

<formbean name=uploadForm type=myuploadUploadActionForm/>

</formbeans>

<actionmappings>

<action path=/upload

type=myuploadUploadTestAction

name=uploadForm

scope=request

>

<forward name=success path=/upload_successjsp/>

</action>

</actionmappings>

<controller maxFileSize=M/>

</strutsconfig></span>

该文件中配置了jsp页面提交后交由哪个Action进行处理的具体信息以及上传成功后的页面跳转信息相信通过应用struts你会发现实现多文件上传会是一件更简单的事情

               

上一篇:MyEclipse中Spring工程使用@Resource注释的问题

下一篇:Spring强制向servlet中注入bean的方法