action:
package com.cstp.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
//固定名字
private List<File> file;
private List<String> fileFileName;
private List<String> fileContentType;
public List<File> getFile() {
return file;
}
public void setFile(List<File> file) {
this.file = file;
}
public List<String> getFileFileName() {
return fileFileName;
}
public void setFileFileName(List<String> fileFileName) {
this.fileFileName = fileFileName;
}
public List<String> getFileContentType() {
return fileContentType;
}
public void setFileContentType(List<String> fileContentType) {
this.fileContentType = fileContentType;
}
public String upload() {
if(file!=null){
for(int i=0;i<this.getFile().size();++i){
String path=ServletActionContext.getServletContext().getRealPath("/upload");
System.out.println(path);
try {
BufferedOutputStream bs=new BufferedOutputStream(new FileOutputStream(new File(path,this.fileFileName.get(i))));
BufferedInputStream bi=new BufferedInputStream(new FileInputStream(this.getFile().get(i)));
byte[] b=new byte[100];
int length=0 ;
do{
length=bi.read(b);
bs.write(b);
}while(length>0);
bi.close();
bs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return SUCCESS;
}
}
struts.properties:(也可以用constant标签定义在struts.xml里)
struts.multipart.parser=jakarta
struts.multipart.saveDir=c:/temp
struts.multipart.maxSize=-1
struts.xml:
<action name="upload" class="com.cstp.action.UploadAction"
method="upload">
<result name="success">/uploadsuccess.jsp</result>
<result name="input">/upload.jsp</result>
</action>
upload.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function addMore(){
var td=document.getElementById("more")
var br=document.createElement("br");
var input=document.createElement("input");
var removeButton=document.createElement("input");
input.type="file";
input.name="file";
removeButton.type="button";
removeButton.value="移除";
removeButton.onclick=function(){
td.removeChild(br);
td.removeChild(input);
td.removeChild(removeButton);
}
td.appendChild(br);
td.appendChild(input);
td.appendChild(removeButton);
}
</script>
</head>
<body>
<form action="upload.action" enctype="multipart/form-data" method="post">
<table>
<tr>
<td id="more"><input type="file" name="file"/><input type="button" value="增加数量" onclick="addMore()" /></td>
</tr>
<tr>
<td align="right"><input type="submit" value="上传" /></td>
</tr>
</table>
</form>
</body>
</html>
uploadsuccess.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:property value="fileFileName"/><br>
<s:property value="fileContentType"/><br>
<s:iterator id="xx" value="%{fileFileName}"><br>
<img alt="" src="./upload/${xx}">
</s:iterator>
</body>
</html>
效果图: