zoukankan      html  css  js  c++  java
  • springmvc的单文件上传

      @RequestMapping("/up")
      public ModelAndView up(MultipartFile myfiles , HttpServletRequest request){
        ModelAndView model = new ModelAndView();
        String result = savePic(request, myfiles);  //调用保存的方法
        if(!StringUtils.isEmpty(result)){      --非必要代码
          System.out.println("上传成功!");
          model.addObject("fileName",result);
          model.setViewName("/picshow");
        }else{
          System.out.println("上传失败!");
        }
        return model;
      }

      public String savePic(HttpServletRequest request,MultipartFile multipartFile){
        try {
          if(!multipartFile.isEmpty()){
            String filePath = request.getServletContext().getRealPath("upPic");  //获取upPic文件夹的路径
            String fileName =new Date().getTime()+multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().indexOf("."),         multipartFile.getOriginalFilename().length());             
            String newFileName = filePath+File.separator+fileName;
            File file = new File(newFileName);                   //创建文件对象
            if(!file.getParentFile().exists()){                    //不存在父路径进行创建
              file.getParentFile().mkdir();
            }
            multipartFile.transferTo(file);                     //进行文件转存
            return fileName;                           //返回存储的文件的名称
          }
        } catch (IllegalStateException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
        return null;
      }

  • 相关阅读:
    正则表达式的总结
    网络搭建的四种方式
    argparse的总结详情
    错误记录
    8x8点阵的原理及代码实现
    __pycache__的认识记录
    浏览器渲染原理及流程
    javascript 中 async/await 的用法
    浏览器的进程和线程
    JS 对象toString 和 valueof 方法
  • 原文地址:https://www.cnblogs.com/qiankun-site/p/5843496.html
Copyright © 2011-2022 走看看