Uploader

Uploader_第1张图片
Paste_Image.png
Uploader_第2张图片
Paste_Image.png
Uploader_第3张图片
Paste_Image.png

使用了Spring的框架+Apache的FileUpload,目前实现的版本为不支持断点续传。根据本人的经验,有几点需要注意:
1调试的时候使用debug的话会出现异常,导致文件接收不正常,如果需要调试的话,用sysout输出结果查看。
2前台使用addData传输的数据只能用字符串,貌似不支持数值型和其他类型的,否则使用request.getParameter()获取不到数据。

Map map = new HashMap();
        if(request.getHeader("content-type")!=null&&"application/x-www-form-urlencoded".equals(request.getHeader("content-type"))){
            return null;//不支持断点续传,直接返回null即可
        }
//将请求转换成
        MultipartHttpServletRequest mRequest=(MultipartHttpServletRequest)request;
        Enumeration ps = mRequest.getParameterNames();
        while(ps.hasMoreElements()){
            String hname = ps.nextElement();
            System.out.println(hname);
            System.out.println(mRequest.getParameter(hname));
        }
        int eventType=0;
        int reportSource=0;
        double  longitude=0;
        double latitude=0;
        if(mRequest.getParameter("eventType")!=null||!"".equals(mRequest.getParameter("eventType").trim()))
            eventType= Integer.parseInt(mRequest.getParameter("eventType"));
        if(mRequest.getParameter("reportSource")!=null||!"".equals(mRequest.getParameter("reportSource").trim()))
            reportSource= Integer.parseInt(mRequest.getParameter("reportSource"));
        String reporter= mRequest.getParameter("reporter");
        String phone= mRequest.getParameter("phone");
        String title= mRequest.getParameter("title");
        String description= mRequest.getParameter("description");
        if(mRequest.getParameter("longitude")!=null||!"".equals(mRequest.getParameter("longitude").trim()))
            longitude= Double.parseDouble(mRequest.getParameter("longitude"));
        if(mRequest.getParameter("latitude")!=null||!"".equals(mRequest.getParameter("latitude").trim()))
            latitude= Double.parseDouble(mRequest.getParameter("latitude"));

        Iterator fns=mRequest.getFileNames();//获取上传的文件列表
        while(fns.hasNext()){
            String s =fns.next();
            System.out.println(s+"==="+mRequest.getFile(s));
//          System.out.println(mRequest.getFile(s));//get file success!!!!!
            MultipartFile mFile = mRequest.getFile(s);  
            if(mFile.isEmpty()){
                map.put("error", "EventAction.picture.failed");
            }else{
                String basePath=Constant.BASEPICUPLOADPATH;
                String dPath= Constant.SDF_PARAM.format(new Date());
                File dir = new File(basePath+dPath);
                if(!dir.exists()){
                    dir.mkdirs();
                }
                String originFileName=mFile.getOriginalFilename();
                String suffix=originFileName.split("\\.")[originFileName.split("\\.").length-1];
                String base64Name=UUID.randomUUID().toString();
                File file =  new File(basePath+dPath,base64Name+"."+suffix);
                try {
                    FileUtils.copyInputStreamToFile(mFile.getInputStream(),file);//存储文件
                } catch (IOException e) {
                    e.printStackTrace();
                }  
            }
        }
        //System.out.println(mRequest.getFileNames());
        map.put("result", "OK");//返回结果
        return map;
    }

你可能感兴趣的:(Uploader)