您的位置:首页 > 其它

动态input file多文件上传到后台没反应的解决方法!!!

2016-04-12 21:18 197 查看
其实我也不太清除具体是什么原因,但是后面就可以了!!!

我用的是springMVC自带的文件上传

1、首先肯定是要有springMVC上传文件的相关配置!

2、前端

  这是动态inputfile上传到后台没反应的写法(页面上写死的上传到后台是可以的)

  这段代码是写在table>>下的form表单里的

  <inputtype="button"name="button"value="添加附件"onclick="addInput()">
<inputtype="button"name="button"value="删除附件"onclick="deleteInput()">
<spanid="upload"></span>
改为
吧上面那段代码换种方式,先写<form>,在写<table>

<divclass="modal-content"style="width:600px">
<formaction="/ecp/mulFileUpload/testFu"method="POST"enctype="multipart/form-data">
<inputtype="hidden"name="topicId"value="${topicId}">
<tableclass="tabletable-bordered">
<tr>
<th>活动参与人数:</th>
<td><inputtype="text"name="peopleCount"/></td>
</tr>
<tr>
<th>活动人均经费:</th>
<td><inputtype="text"name="perPrice"/></td>
</tr>
<tr>
<th>上传图片/附件:</th>
<td>
<inputtype="file"name="myfiles"/>
<spanid="upload"></span>
</td>
</tr>
<tr>
<td>
<inputtype="button"name="button"value="添加图片/附件"onclick="addInput()">
<inputtype="button"name="button"value="删除图片/附件"onclick="deleteInput()">
</td>
<%--<td><buttonclass="btnbtn-success"onclick="formSubmit()"></button></td>--%>
<td><inputtype="submit"class="btnbtn-success"/></td>
</tr>

</table>
</form>
</div>

这样就可以了,说实话我也不知道为什么(!!!!)

3、js代码

varattachName="myfiles";

functionaddInput(){
createInput(attachName);
}
functiondeleteInput(){
removeInput();
}

functioncreateInput(name){
varaElement=document.createElement("input");
aElement.name=name;
aElement.type="file";

varspanElement=document.getElementById("upload");
/*if(document.getElementById("upload").insertBefore(aElement,spanElement.nextSibling)==null){
returnfalse;
}*/
if(document.getElementById("upload").appendChild(aElement)==null){
returnfalse;
}
returntrue;
}

functionremoveInput(){
varaElement=document.getElementById("upload");
if(aElement.removeChild(aElement.lastChild)==null){
returnfalse;
}

returntrue;
}

4、Java代码

packagecom.ibm.db.controller;

importcom.ibm.db.service.IMulFileUploadService;
importcom.ibm.db.service.ITopicService;
importorg.apache.commons.io.FileUtils;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestParam;
importorg.springframework.web.bind.annotation.ResponseBody;
importorg.springframework.web.bind.annotation.RestController;
importorg.springframework.web.multipart.MultipartFile;

importjavax.annotation.Resource;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjava.io.File;
importjava.io.IOException;
importjava.text.DateFormat;
importjava.text.SimpleDateFormat;
importjava.util.Date;

/**
*Createdbyzmlon16-4-11.
*/
@RestController
@RequestMapping(value="/ecp/mulFileUpload")
publicclassmulFileLoadifyController{

@Resource(name=IMulFileUploadService.SERVICE_NAME)
privateIMulFileUploadServicetopicService;

@RequestMapping(value="/testFu")
publicStringaddUser(@RequestParamMultipartFile[]myfiles,HttpServletRequestrequest)throwsIOException{

DatedataTime=newDate();
//保存该活动贴的相关信息,材料提交状态改为1
topicService.insertIno(topicId,1,peopleCount,perPrice,dataTime);

//如果只是上传一个文件,则只需要MultipartFile类型接收文件即可,而且无需显式指定@RequestParam注解
//如果想上传多个文件,那么这里就要用MultipartFile[]类型来接收文件,并且还要指定@RequestParam注解
//并且上传多个文件时,前台表单中的所有<inputtype="file"/>的name都应该是myfiles,否则参数里的myfiles无法获取到所有上传的文件
//判断file数组不能为空并且长度大于0
if(myfiles!=null&&myfiles.length>0){

//循环获取file数组中得文件
for(inti=0;i<myfiles.length;i++){
MultipartFilefile=myfiles[i];
StringuploadContentType=file.getContentType();
StringexpandedName="";
if(uploadContentType.equals("imagepeg")
||uploadContentType.equals("image/jpeg")){
//IE6上传jpg图片的headimageContentType是imagepeg,而IE9以及火狐上传的jpg图片是image/jpeg
expandedName=".jpg";
}elseif(uploadContentType.equals("image/png")
||uploadContentType.equals("image/x-png")){
//IE6上传的png图片的headimageContentType是"image/x-png"
expandedName=".png";
}elseif(uploadContentType.equals("image/gif")){
expandedName=".gif";
}elseif(uploadContentType.equals("image/bmp")){
expandedName=".bmp";
}

//保存文件
saveFile(file,expandedName,request);
}
}
return"uploadSuccess";
//return"redirect:/list.html";
}

/***
*保存文件
*@paramfile
*@return
*/
privatebooleansaveFile(MultipartFilefile,StringexpandedName,HttpServletRequestrequest){
DateFormatdf=newSimpleDateFormat(TopicController.DEFAULT_SUB_FOLDER_FORMAT_AUTO);
StringfileName=df.format(newDate());
//判断文件是否为空
if(!file.isEmpty()){
try{
StringfilePath="";
//文件保存路径
if(expandedName!=null&&!expandedName.equals("")){
//如果是图片
filePath=request.getSession().getServletContext().getRealPath("/")+"upload/img/"
+fileName+expandedName;
}else{
StringOriginalFilename=file.getOriginalFilename();
Stringsuffix=OriginalFilename.substring(OriginalFilename.lastIndexOf(".")+1);
filePath=request.getSession().getServletContext().getRealPath("/")+"upload/file/"
+fileName+"."+suffix;
}

FiletargetFile=newFile(filePath);
if(!targetFile.exists()){
targetFile.mkdirs();
}

//转存文件
file.transferTo(targetFile);
returntrue;
}catch(Exceptione){
e.printStackTrace();
}
}
returnfalse;
}
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: