java web 附件_Java web项目实现多附件上传

标签:

jsp代码:

               tianjia.png

添加附件   每个附件大小不能超过50M

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);

}

}

标签:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值