UploadAction.java
public class UploadAction{
public String addAct(){
StringBuffer sbf=new StringBuffer();
if(file!=null && file.length > 0){
for(int i=0;i<file.length;i++){
try{
String filename=fileFileName[i];
String fileExt = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
InputStream is = new FileInputStream(file[i]);
File destFile = new File(root, newFileName);
OutputStream os = new FileOutputStream(destFile);
byte[] buffer = new byte[50000];
int length = 0;
while (-1 != (length = is.read(buffer))) {
os.write(buffer, 0, length);
}
sbf.append(newFileName);
if(i < file.length-1){
sbf.append(",");
}
}catch(Exception e){
log.debug("上传文档失败");
}
}
}
}
private File[] file;
/*
* 这里定义的fileFileName一定要是xxxFileName的形式,否则无法取到文件的文件名.
* 其中xxx必须与上面定义的File类型的变量一致,如果上面定义的是File img,那么这里就
* 定义为String imgFileName
*/
private String[] fileFileName;
/*
* 这里定义的是文件的类型,如果不需要获取文件类型的话,可以不定义.
* 命名规则跟xxxFileName类似,这里一定要定义成xxxContentType形式.
*/
private String[] fileContextType;
//下载文件
public InputStream getDownExclFile() {
File file = new File(PluginPropertiesLoader.getPluginResource()+"/addins/excel/"+fliename);
try {
InputStream is = new FileInputStream(file);
return is;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
public void setDownExclFile(InputStream downExclFile) {
this.downExclFile = downExclFile;
}
public File[] getFile() {
return file;
}
public String[] getFileFileName() {
return fileFileName;
}
public String[] getFileContextType() {
return fileContextType;
}
public void setFile(File[] file) {
this.file = file;
}
public void setFileFileName(String[] fileFileName) {
this.fileFileName = fileFileName;
}
public void setFileContextType(String[] fileContextType) {
this.fileContextType = fileContextType;
}
}
jsp页面
<form action="harmless_addInfo.html" method="post" onSubmit="return Validator.Validate(this)" enctype="multipart/form-data">
<input type="file" name="file" style="width:240px;" οnfοcus="javascript:setfocus(this);" οnblur="javascript:setblur(this);" seq="1" class="seq" />
<a href="#" οnclick="addtr()"> <img src="images/edit_add.png"></img> 添加</a> <br/>
</p>
<p>(支持jpg、png或gif格式,500x500像素以内,1M以下)</p>
</form>
注意:<form>中的“enctype="multipart/form-data"”不能少,method="post",<input>中的name必须要和action中的定义的file要一样,如:<input type="file" name="file">在action中private File[] file;
js (添加多个上传框,可以删除)
function addtr(){
var tdNameLast=$("input[class=seq]:last");
var seqStr=tdNameLast.attr("seq");
var seq=parseInt(seqStr);
$("#file").append('<span id="'+(seq+1)+'"><input type="file" name="file" style="width:240px;" οnfοcus="javascript:setfocus(this);" οnblur="javascript:setblur();" class="seq" seq="'+(seq+1)+'"/><a href="#" οnclick="addtr()"> <img src="images/edit_add.png" ></img> 添加</a> <a href="#" οnclick="deltr(this)" seq="'+(seq+1)+'" ><img src="images/edit_remove.png"></img> 删除</a></span><br/>');
}
function deltr(obj){
alert(obj);
var seq = $(obj).attr("seq");
alert("seq------------------------------"+seq);
$("span[id="+seq+"]").remove();
}
struts.xml
自行配置