首页 > 代码库 > http 上传文件
http 上传文件
@RequestMapping(method=RequestMethod.POST,value = "http://www.mamicode.com/upload") public ModelAndView processImageUpload(@RequestParam(value="http://www.mamicode.com/groupname",required=false) String group, @RequestParam(value="remotefilename",required=false) String remotefilename, @RequestParam(value="extname",required=true) String extname, @RequestParam("file") MultipartFile file ,@RequestParam(value="http://www.mamicode.com/format",required=false) String format // 关键就是这句话起了作用 ){ Response<FastDFSFile> response = new Response<FastDFSFile>(); ReturnResult<FastDFSFile> returnResult = new ReturnResult<FastDFSFile>(); ResponseHeader responseHeader = new ResponseHeader(); boolean appendFlag = false; String[] results = null; try { if (!file.isEmpty()) { if(group != null && !group.equals("") && remotefilename != null && !remotefilename.equals("")){ results = new String[2]; results[0] = group; results[1] = remotefilename; appendFlag = true; }else appendFlag = false; byte[] file_buff = file.getBytes(); String[] fDinfo = FastDFS.getServer().upload(appendFlag, results, file_buff, extname); if(fDinfo.length == 2){ List<FastDFSFile> fileList = new ArrayList<FastDFSFile>(); FastDFSFile info = new FastDFSFile(); info.setGroupname(fDinfo[0]); info.setRemotefilename(fDinfo[1]); fileList.add(info); responseHeader.setStatus(0); returnResult.setNumFound(1); returnResult.setStart(1); returnResult.setFilelist(fileList); }else if(fDinfo.length == 1){ throw new UFEException(fDinfo[0]); }else{ throw new UFEException("no return fastDFS!"); } }else{ RError er = new RError(); er.setInfo("No file!"); er.setType(1); responseHeader.setError(er); responseHeader.setStatus(1); } } catch (Exception e1) { RError er = new RError(); if(e1.getCause() == null) er.setInfo(e1.getMessage()); else er.setInfo(e1.getCause().getMessage()); er.setType(1); responseHeader.setStatus(1); responseHeader.setError(er); } response.setResult(returnResult); response.setResponseHeader(responseHeader); if(format != null && format.equals("json")) return new ModelAndView(TypeDef.JSON_VIEW_NAME, "object", response); else return new ModelAndView(TypeDef.XML_VIEW_NAME, "object", response); }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。