标签:
jsp代码:
<div class="col-md-8 col-sm-8">
<a href="###" onclick="addComponent();">
<img class="lessimg" src="${ctx}/image/Activity/slider/tianjia.png" height="15" width="15"/>
添加附件</a><font color="red"> 每个附件大小不能超过50M</font>
</div>
js代码:
function addComponent()
{
var flag = document.getElementById("numsize").value;
if(flag>9){
alert("您添加的附件个数不能超过10个!");
return false;
}else{
fcc = document.createElement("p");
document.getElementById("files").appendChild(fcc);
//计数器,来计数file的个数
var numsize = document.getElementById("numsize").value;
var numsize = Number(numsize)+Number(1);
document.getElementById("numsize").value=numsize;
//
var fc = document.getElementById("count").value;
var count = Number(fc)+Number(1);
document.getElementById("count").value=count;
var uploadHTML = document.createElement("input");
uploadHTML.setAttribute("type", "file");
uploadHTML.setAttribute("name", "filegg");
uploadHTML.setAttribute("id", count);
uploadHTML.setAttribute("style", "max-width:300px;word-break:break-all;display:inline-block;");
document.getElementById("files").appendChild(uploadHTML);
var a = document.createElement("a");
//var node = document.createTextNode("删除");
//a.appendChild(node);
a.setAttribute("href","###");
a.setAttribute("id", "btn("+count+")");
a.setAttribute("style", "display:inline-block;");
a.setAttribute("onclick","fc("+count+")");
var img = document.createElement("img");
img.setAttribute("src","${ctx}/image/Activity/slider/delete.png");
//img.setAttribute("id", "img("+count+")");
img.setAttribute("style", "display:inline-block;");
img.setAttribute("height", "15px");
img.setAttribute("width", "15px");
img.setAttribute("class", "lessimg");
a.appendChild(img);
document.getElementById("files").appendChild(a);
}
}
后台代码:
if(filegg!=null){
//上传附件
for (int i=0; i < filegg.size(); i++){
ActivityFile entityfileTemp = new ActivityFile();
//存放路径
//String path ="/upload/"+entity.getRandom()+"/"+fileggFileName.get(i);
String randomUuid = UUID.randomUUID().toString();
String extendf=fileggFileName.get(i).substring(fileggFileName.get(i).lastIndexOf("."));
String path ="/upload/"+random+"/"+randomUuid+extendf;
String anotherName = randomUuid+extendf;
//换之前的文件的路径
String totalFilePath=dstPath1 + File.separator +fileggFileName.get(i);
//换之后文件的路径
String newTotalFilePath=dstPath1 + File.separator + randomUuid+extendf;
//换之前的文件路径(即文件)
File totalActivityFile=new File(totalFilePath);//获取存储文件的目标地址
//换之后的文件路径(即文件)
File newTotalActivityFile=new File(newTotalFilePath);//获取存储文件的目标地址
//换文件
totalActivityFile.renameTo(newTotalActivityFile);
String activityFileName=this.fileggFileName.get(i);
/*userService.upload(filegg.get(i), totalActivityFile); //上传附件
* */
userService.upload(filegg.get(i), newTotalActivityFile);
entityfileTemp.setRounte(path);
entityfileTemp.setRandomf(random);
entityfileTemp.setAnotherfileName(anotherName);
entityfileTemp.setFileName(activityFileName);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");//可以方便地修改日期格式
String fileTime = dateFormat.format(new Date());
entityfileTemp.setFileTime(fileTime);
entityfileTemp.setFileFlag("0");
entityfileTemp.setDeptCode("0");
activityFile.add(entityfileTemp);
}
}
标签:
原文地址:http://www.cnblogs.com/mgstone/p/5150378.html