java

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

基于Struts1.2的动态多文件上传


发布日期:2021年07月31日
 
基于Struts1.2的动态多文件上传

首先是webxml配置

<?xml version= encoding=ISO ?>

<!DOCTYPE webapp PUBLIC //Sun Microsystems Inc//DTD Web Application //EN app__dtd>

<webapp>

<servlet>

<servletname>action</servletname>

<servletclass>orgapachestrutsactionActionServlet</servletclass>

<initparam>

<paramname>config</paramname>

<paramvalue>/WEBINF/strutsconfigxml</paramvalue>

</initparam>

<loadonstartup></loadonstartup>

</servlet>

<servletmapping>

<servletname>action</servletname>

<urlpattern>*do</urlpattern>

</servletmapping>

</webapp>

然后是strutsconfigxml

<?xml version= encoding=UTF?>

<!DOCTYPE strutsconfig PUBLIC //Apache Software Foundation//DTD Struts Configuration //EN config__dtd>

<strutsconfig>

<datasources />

<formbeans >

<formbean name=multiUploadForm type=comhbstrutsformMultiUploadForm />

</formbeans>

<globalexceptions />

<globalforwards />

<actionmappings >

<action

attribute=multiUploadForm

name=multiUploadForm

path=/multiUpload

scope=request

type=comhbstrutsactionMultiUploadAction

validate=false />

</actionmappings>

<messageresources parameter=comhbstrutsApplicationResources />

</strutsconfig>

然后是页面testjsp这段就是网上COPY的动态生成页面

<%@ page language=java pageEncoding=utf%>

<%@ taglib uri=bean prefix=bean %>

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

<%@ taglib uri=logic prefix=logic %>

<%@ taglib uri=tiles prefix=tiles %>

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

<html:html>

<head>

<title>

multiUploadDemo

</title>

<script language=javascript>

var num = ;

function upload(){

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

multiUploadFormsubmit();

}

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;

}

</script>

<body bgcolor=#ffffff>

<div id=status></div>

<html:form method=post action=/multiUploaddo enctype=multipart/formdata target=hidden_frame>

<table id=tb>

</table>

</html:form>

<iframe name=hidden_frame id=hidden_frame ></iframe>

<input type=button name=btnAddFile value=Add File onclick=additem(tb)/>

<input type=button name=btnUpload value=upload onclick=upload()/>

</body>

</html:html>

下面是FORM和ACTION

我把下面个文件放在FORM包中

UploadFile java

package comhbstrutsform;

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;

}

}

MultiUploadForm java

package comhbstrutsform;

import javautilArrayList;

import javautilList;

import orgapachestrutsactionActionForm;

import comhbstrutsUploadFile;

public class MultiUploadForm extends ActionForm {

private List myFiles;

public MultiUploadForm(){

myFiles = new ArrayList();

myFilesadd(new UploadFile());

}

public List getMyFiles() {

return myFiles;

}

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

public UploadFile getUploadFile(int index){

int size = myFilessize();

if(index>size){

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

myFilesadd(new UploadFile());

}

}

return (UploadFile)myFilesget(index);

}

public void setMyFiles(List myFiles) {

thismyFiles = myFiles;

}

}

最后是action

package comhbstrutsaction;

import javaioIOException;

import javaioPrintWriter;

import javautilList;

import javaxservlethttpHttpServletRequest;

import javaxservlethttpHttpServletResponse;

import orgapachestrutsactionAction;

import orgapachestrutsactionActionForm;

import orgapachestrutsactionActionForward;

import orgapachestrutsactionActionMapping;

import orgapachestrutsuploadFormFile;

public class MultiUploadAction extends Action {

public ActionForward execute(ActionMapping mapping ActionForm form

HttpServletRequest request HttpServletResponse response) {

MultiUploadForm multiUploadForm = (MultiUploadForm) form;

List myFiles = multiUploadFormgetMyFiles();

String fileStr = ;

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

UploadFile uploadFile = (UploadFile) myFilesget(i);

FormFile file = uploadFilegetFile();

if (file == null) {

Systemoutprintln(file is null);

} else {

// 能运行到这里就可以使用单个文件上传的方法进行上传了循环而已

Systemoutprintln(filename>>>>>>>>>> + filegetFileName());

Systemoutprintln(file size>>>>>>>>>> + filegetFileSize());

fileStr += filename::: + filegetFileName() + file size:::

+ filegetFileSize();

}

}

// 返回文本

try {

responsesetHeader(ContentType text/html;charset=gbk);

PrintWriter out = responsegetWriter();

outwrite(<script>parentcallback(upload file success + fileStr

+ )</script>);

} catch (IOException e) {

eprintStackTrace();

}

return null;

}

}

这个是我从现在写好的里截取的一部分可能还是有不正确的地方但是大体的思想是在这了 struts 我用的jar包

               

上一篇:Hibernate属性查询简介

下一篇:MyEclipse使用之应用指南