bootstrap多图片上传实现

前端代码

 

需要引入fileinput.js

后端代码

@SuppressWarnings("resource")
@RequestMapping(value = "/uploader.do")
public Map upload(HttpServletRequest request, HttpServletResponse response) throws IOException{
response.setCharacterEncoding("UTF-8");
Map json = new HashMap();
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        
        /** 页面控件的文件流* */
        MultipartFile multipartFile = null;
        Map map =multipartRequest.getFileMap();
         for (Iterator i = map.keySet().iterator(); i.hasNext();) {
                Object obj = i.next();
                multipartFile=(MultipartFile) map.get(obj);


               }
        /** 获取文件的后缀* */
        String filename = multipartFile.getOriginalFilename();


        InputStream inputStream;
        String basePath=servletContext.getRealPath("/").substring(0, servletContext.getRealPath("/").lastIndexOf("bspheis-wechat"))+"/upload/";
        String outPath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
        String newVersionName = "";
        String fileMd5 = "";
        byte[] data = new byte[1024];
        int len = 0;
        FileOutputStream fileOutputStream = null;
        
        try {


            inputStream = multipartFile.getInputStream();
            fileOutputStream = new FileOutputStream(basePath+filename);
            while ((len = inputStream.read(data)) != -1) {
            fileOutputStream.write(data, 0, len);
            }
           
        } catch (Exception e) {
            e.printStackTrace();
        }
             json.put("newVersionName", filename);
        json.put("fileMd5", fileMd5);
        json.put("message", "图片上传成功");
        json.put("status", true);
        return json;


}


你可能感兴趣的:(java)