码迷,mamicode.com
首页 > 其他好文 > 详细

http 上传文件

时间:2014-08-19 14:27:24      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   文件   for   

@RequestMapping(method=RequestMethod.POST,value = "/upload")   
    public ModelAndView processImageUpload(@RequestParam(value="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="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);
    }

 

http 上传文件,布布扣,bubuko.com

http 上传文件

标签:style   blog   http   color   os   io   文件   for   

原文地址:http://www.cnblogs.com/dorothychai/p/3921875.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!