leizaolong
|
文件上传控件,后台上传成功了,但是前台却提示失败是什么原因导致的 后台代码: /***上传照片文件到指定位置 * * * @param request * @param response * @return */ @RequestMapping(value = "/uploadPhotos.do") public void uploadBinFile(HttpServletRequest request, HttpServletResponse response) { MobileCommonResult cr = new MobileCommonResult(); // 服务器根路径 String url = GmtLoadConfig.getValueByKey(GmtConstants.PARK_2G_BIN_PATH); Map<String , String > map = new HashMap<String, String>(); String filename = ""; try{ // 创建一个通用的多部分解析器 CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver( request.getSession().getServletContext()); // 判断 request 是否有文件上传,即多部分请求 if (multipartResolver.isMultipart(request)) { // 转换成多部分request MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; // 取得request中的所有文件名 Iterator<String> iter = multiRequest.getFileNames(); while (iter.hasNext()) { // 取得上传文件 MultipartFile file = multiRequest.getFile(iter.next()); if (file != null) { // 取得当前上传文件的文件名称 String myFileName = file.getOriginalFilename(); // 如果名称不为“”,说明该文件存在,否则说明该文件不存在 // if (myFileName.trim() != "") { if(StringUtils.isNotEmpty(myFileName)){ // 重命名上传后的文件名 // MobileMerchantUtil.getUploadImgName() // fileName = MobileMerchantUtil.getUploadImgName(file.getOriginalFilename()); // 将文件保存路径保存到List中 //pathList.add(path + fileName); filename = myFileName;
File localFile = new File( url + myFileName); file.transferTo(localFile); } } } cr.setResult(MobileCommonConstant.SUCCESS); map.put("name", filename); cr.setData(map);
} else { cr.setMsg("请上jsp/png文件"); cr.setResult(MobileCommonConstant.WARNING); } }catch (IllegalStateException e) { cr.setMsg("上传文件失败"); cr.setResult(MobileCommonConstant.WARNING); } catch (IOException e) { cr.setMsg("上传文件失败"); cr.setResult(MobileCommonConstant.WARNING); } mobileToJson(cr, request, response); }
前台代码: <div id="uploaderExample" class="uploader"> <div class="file-list" data-drag-placeholder="请拖拽文件到此处"></div> <button type="button" class="btn btn-primary uploader-btn-browse"><i class="icon icon-cloud-upload"></i> 选择图片</button> <button type="button" class="btn btn-primary" onclick="addConsPersonInfo()">保存信息</button> </div>
//文件上传 $('#uploaderExample').uploader({ autoUpload: true, // 当选择文件后立即自动进行上传操作 url: '${ctx}/uploadPhotos.do', // 文件上传提交地址 /* onUploadFile: function(file) { alert('上传成功'+file); } */ });
|
2018-10-12 18:51:48
leizaolong 最后编辑, 2018-10-12 19:05:21
1/1