一篇关于SpringMVC 传统文件上传的方法
2016-09-29 16:23
176 查看
一、界面效果
![](https://images.cnblogs.com/cnblogs_com/royi123/757294/o_QQ%e6%88%aa%e5%9b%be20160929161355.png)
二、html代码
三、后台java代码
此代码为form表单提交,现改需要改为js提交,故做记录
一个用于ajax上传的js插件
http://files.cnblogs.com/files/royi123/ajaxfileupload_JS_File.rar
![](https://images.cnblogs.com/cnblogs_com/royi123/757294/o_QQ%e6%88%aa%e5%9b%be20160929161355.png)
二、html代码
<legend>上传APK文件</legend> <form action="<%=basePath%>/apks/commitApk" class="form-horizontal" method="post" enctype="multipart/form-data"> <input name="cityCode" value="${s0.paramValue}" type="hidden"/> <input name="productCode" value="${s.paramValue}" type="hidden"/> <div class="form-group"> <label for="ticket-message" class="col-sm-3 control-label col-md-1">版本号</label> <div class="col-md-4"> <input type="text" class="form-control" name="versionCode" id="versionCode" placeholder="请输入整数"> </div> </div> <div class="form-group"> <label for="ticket-message" class="col-sm-3 control-label col-md-1">版本名</label> <div class="col-md-4"> <input type="text" class="form-control" name="versionName" id="versionName" placeholder="0.0.0.1"> </div> </div> <div class="form-group"> <label for="ticket-message" class="col-sm-3 control-label col-md-1">描述</label> <div class="col-md-4"> <textarea class="form-control" name="versionDesc" id="versionDesc" rows="5" cols="30" placeholder="版本描述"></textarea> </div> </div> <div class="form-group"> <label for="ticket-message" class="col-sm-3 control-label col-md-1">文件</label> <!-- <label for="ticket-attachment" class="col-sm-3 control-label">请选中apk文件</label> --> <div class="col-md-4"> <input type="file" name="apkFile" id="apkFile"> <p class="help-block"><em>文件类型: .apk</em></p> </div> </div> <div class="form-group"> <label for="ticket-message" class="col-sm-3 control-label col-md-1"></label> <div class="col-md-4"> <input id="apkSubmitBtn" type="submit" class="btn btn-danger"></input> </div> </div> </form>
三、后台java代码
@RequestMapping(value = "/commitApk", method = RequestMethod.POST) public @ResponseBody ModelAndView commitApk(@RequestParam("cityCode") String cityCode,@RequestParam("productCode") String productCode, @RequestParam("apkFile") CommonsMultipartFile[] apkFile,@RequestParam("versionCode") int versionCode,@RequestParam("versionName") String versionName, @RequestParam("versionDesc") String versionDesc, HttpSession session) { FileOutputStream out = null; FileInputStream in = null; try { String appPath = System.getProperty("root"); appPath = appPath.substring(0, appPath.indexOf("ExceptionManageSystem")); StringBuffer buffer = new StringBuffer(appPath + com.tongyan.ems.common.Constants.APKMANAGER_FILES_PAHT); buffer.append(productCode).append("\\").append(cityCode).append("\\"); File fileDir = new File(buffer.toString()); if(!fileDir.exists()) { fileDir.mkdirs(); } buffer.append(apkFile[0].getFileItem().getName()); File file = new File(buffer.toString()); if(!file.exists()) { file.createNewFile(); } else { file.delete();//如果存在就删除重新上传 } out = new FileOutputStream(file); in = (FileInputStream)apkFile[0].getInputStream(); int read = 0; byte[] b = new byte[1024]; while((read = in.read(b)) != -1) { out.write(b, 0, read); } //数据入库 ApkManagerPo apkManagerPo = new ApkManagerPo(); apkManagerPo.setApkCode(UUID.randomUUID().toString()); apkManagerPo.setCreateDate(new SimpleDateFormat(Constants.DATE_FORMAT).format(new Date())); apkManagerPo.setProductCode(productCode); apkManagerPo.setCustomerCode(cityCode); apkManagerPo.setVersionCode(versionCode); apkManagerPo.setVersionName(versionName); apkManagerPo.setVersionDesc(versionDesc); if(session.getAttribute("User") != null) { UserPo user = (UserPo)session.getAttribute("User"); apkManagerPo.setUserId(user.getUserId()); }else { apkManagerPo.setUserId(""); } apkManagerPo.setApkRoute(com.tongyan.ems.common.Constants.FEEDBACK_FILES_PAHT + productCode + "\\" + cityCode + "\\" + apkFile[0].getFileItem().getName());//文件夹放在webApp下面 apkManagerPo.setApkPath(com.tongyan.ems.common.Constants.FEEDBACK_FILES_URL + productCode + "/" + cityCode + "/" + apkFile[0].getFileItem().getName()); apkService.addApkVersion(apkManagerPo); } catch (Exception e) { e.printStackTrace(); } finally { try { if(out != null) { out.close(); } if(in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } } return null; }
此代码为form表单提交,现改需要改为js提交,故做记录
一个用于ajax上传的js插件
http://files.cnblogs.com/files/royi123/ajaxfileupload_JS_File.rar
相关文章推荐
- 关于SpringMVC上传文件的方法
- 关于Html动态添加上传文件控件inputFile(附件)及清除已选择的文件的几个方法
- Java中关于文件上传的基本处理方法Commons-FileUpload
- 关于上传文件时,提示控制台信息存取失败的解决方法
- SpringMVC 两种上传文件方法
- 年前最后一篇----关于DOC,PDF,PPT怎么逃过网络各类文库相同文件的审核 重金悬赏高效处理大量文档的方法
- 分享一篇关于C#对文件操作的日志,方法很全
- 关于初学者上传文件到github的方法
- 关于初学者上传文件到github的方法
- 关于ueditor 在struts2 中 上传图片 ,未找到上传文件 问题的解决方法
- 关于初学者上传文件到github的方法
- php中关于普通表单多文件上传的处理方法
- 一篇关于struts2文件上传的好文章
- 关于ext与springMVC中上传文件并以JSON格式返回入STORE的问题
- springmvc上传文件配置方法
- springmvc Excel文件上传,使用ajaxSubmit方法进行文件上传,使用弹出层弹出一个框(jquery layer),点击进行下载
- C#关于文件上传下载方法
- 关于初学者上传文件到github的方法
- 关于多文件上传解决方法
- 关于SpringMVC的文件上传