当前位置 主页 > 服务器问题 > win服务器问题汇总 >
一、html代码
<div class="layui-form-item"> <label class="layui-form-label">上传附件:</label> <div class="layui-input-block doc-litpic"> <button type="button" name="avatar" class="layui-btn layui-btn-sm" data-url="/admin/archives/upload.html" ><i class="layui-icon"></i>本地上传</button> <a href=""></a> <div class="larryms-img-view"> </div> </div> </div>
二、js代码
upload.render({ accept: 'file', elem: '#larry-litpic', url: interface_cms_article_upload, field: 'fileNames', done : function (res, index, upload) { if(res.code != 200){ layer.open({ icon : 2, skin : "layui-layer-molv", content : res.msg }); }else{ layer.open({ icon : 1, skin : "layui-layer-molv", content : res.msg }); $('#upload-filename-display').text(res.filename); $("input[name='fileId']").val(res.filename); } }, error : function (res) { } });
三、后台controller:
@RequestMapping(value = "/upload") @ApiOperation(value = "本地文件上传",notes ="本地文件上传" ) public Map uploadfunction(HttpServletRequest request, HttpServletResponse response){ //创建文件对象并获取请求中的文件对象 MultipartFile file = null; Map resultData = new HashMap(); try{ MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request; file = mRequest.getFile("fileNames"); //判断上传非空 if(null == file) { resultData.put("code",0); resultData.put("msg","上传文件失败"); resultData.put("filename",file.getOriginalFilename()); return resultData; } //上传需要导入数据的文件 //用来检测程序运行时间 long startTime=System.currentTimeMillis(); System.out.println("上传的文件名为:"+file.getOriginalFilename()); String fileName = file.getOriginalFilename(); InputStream inputStream = file.getInputStream(); String hostName = uploadUtil.getHostname(); String username = uploadUtil.getUsername(); String password = uploadUtil.getPassword(); String targetPath = uploadUtil.getTargetPath(); String suffix = cmsArticleService.getSuffix(fileName); fileName = cmsArticleService.upload(hostName,username,password,targetPath,suffix,inputStream); //计算上传时间 long endTime=System.currentTimeMillis(); String uploadTime = String.valueOf(endTime-startTime); System.out.println("上传所用时间:"+uploadTime+"ms"); resultData.put("code",200); resultData.put("msg","上传文件成功"); resultData.put("filename",fileName); return resultData; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }