现在工作需要用到批量上传 ?希望大家给个例子看下
upload.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<script type="text/javascript">
function addMore()
{
var td = document.getElementById("more");
var br = document.createElement("br");
var input = document.createElement("input");
var button = document.createElement("input");
input.type = "file";
input.name = "file";
button.type = "button";
button.value = "Remove";
button.onclick = function()
{
td.removeChild(br);
td.removeChild(input);
td.removeChild(button);
}
td.appendChild(br);
td.appendChild(input);
td.appendChild(button);
}
</head>
<body>
<table align="center" width="50%">
<tr>
<td>
<s:fielderror cssStyle="color:red" />
</td>
</tr>
</table>
<s:form action="fileupAction" theme="simple"
enctype="multipart/form-data" method="post">
<table align="center" width="50%" border="1">
<tr>
<td>
username
</td>
<td>
<s:textfield name="username"></s:textfield>
</td>
</tr>
<tr>
<td>
password
</td>
<td>
<s:password name="password"></s:password>
</td>
</tr>
<tr>
<td>
file
</td>
<td id="more">
<s:file name="file" disabled="false"></s:file>
<input type="button" value="Add More.." onclick="addMore()">
</td>
</tr>
<tr>
<td>
<s:submit value=" submit "></s:submit>
</td>
<td>
<s:reset value=" reset "></s:reset>
</td>
</tr>
</table>
</s:form>
</body>
工作就不要偷懒
google一下多的是
struts.xml
<action name="fileupAction" class="test.actions.FileupAction">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
image/bmp,image/png,image/gif,image/jpeg,application/zip
</param>
<param name="maximumSize">200000000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<param name="savePath">/upload</param>
<result name="success">/upload_result.jsp</result>
<result name="input">/upload.jsp</result>
</action>
package test.actions;
import java.io.*;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileupAction extends ActionSupport
{
private File[] file;
private String[] fileContentType;
private String[] fileFileName;
private String savePath;
public String getSavePath()
{
return savePath;
}
public void setSavePath(String savePath)
{
this.savePath = "d:" + savePath;
}
public File[] getFile()
{
return file;
}
public void setFile(File[] file)
{
this.file = file;
}
public String[] getFileContentType()
{
return fileContentType;
}
public void setFileContentType(String[] fileContentType)
{
this.fileContentType = fileContentType;
}
public String[] getFileFileName()
{
return fileFileName;
}
public void setFileFileName(String[] fileFileName)
{
this.fileFileName = fileFileName;
}
@Override
@SuppressWarnings("unchecked")
public String execute() throws Exception
{
System.out.println(savePath);
ActionContext.getContext().getSession().put("savePath", savePath);
String fileName = "[";
if (file == null)
{
this.addFieldError("file", this.getText("file.null"));
return INPUT;
}
for (int i = 0; i < file.length; i++)
{
File uploadFile = file[i];
InputStream is = new FileInputStream(uploadFile);
OutputStream os = new FileOutputStream(new File(savePath,
fileFileName[i]));
byte[] buffer = new byte[1024];
int length = 0;
while ((length = is.read(buffer)) > 0)
{
os.write(buffer, 0, length);
}
fileName = fileName + fileFileName[i];
is.close();
os.close();
}
fileName = fileName + "]";
ActionContext.getContext().getSession().put("uploadFileName", fileName);
return SUCCESS;
}
}
struts.xml
在struts.xml中可以添加下面这段代码,来改变上传文件大小的限制
我自己么有加注释,希望你能看懂