动态input file多文件上传到后台没反应的解决方法!!!
2016-04-12 21:18
197 查看
其实我也不太清除具体是什么原因,但是后面就可以了!!!
我用的是springMVC自带的文件上传
1、首先肯定是要有springMVC上传文件的相关配置!
2、前端
这是动态inputfile上传到后台没反应的写法(页面上写死的上传到后台是可以的)
这段代码是写在table>>下的form表单里的
<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;
}
}
我用的是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;
}
}
相关文章推荐
- 关于继承,类,抽象类,接口的实际操作
- 广播接收器的使用
- hibernate入门
- Struts2 入门学习笔记(二)——拦截器应用
- 欢迎使用CSDN-markdown编辑器
- [BZOJ 1420 && BZOJ 1319]
- dp让宝宝疯掉
- 基于LBS功能应用的Geohash方案
- 阅读器关闭时尝试调用 Read 无效
- Intellij IDEA 的使用
- 持续集成Jenkins环境配置+使用
- 机器学习 笔记(三) 到底选择哪种机器学习算法来分类
- hdu 1102A + B Problem II
- RabbitMQ Spring AMQP
- android NDK编程:使用posix多线程与mutex相互排斥同步
- Java设计模式系列之迭代器模式
- CDISC SDTM DM domain 学习笔记
- FLV 结构分析
- 1214 线段覆盖——http://codevs.cn/problem/1214/
- inode结构体及软链接和硬链接