@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;
  }

springmvc的单文件上传的更多相关文章

  1. SpringMVC单文件上传、多文件上传、文件列表显示、文件下载(转)

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 本文详细讲解了SpringMVC实例单文件上传.多文件上传.文件列表显示.文件下载. 本文工程 ...

  2. [转]SpringMVC单文件上传、多文件上传、文件列表显示、文件下载

    一.新建一个Web工程,导入相关的包 springmvc的包+commons-fileupload.jar+connom-io.jar+commons-logging,jar+jstl.jar+sta ...

  3. springMVC数据校验与单文件上传

    spring表单标签:    <fr:from/> 渲染表单元素    <fr:input/>输入框组件    <fr:password/>密码框组件标签    & ...

  4. SpringMvc入门五----文件上传

      知识点: SpringMvc单文件上传 SpringMvc多文件上传   这里我直接演示多文件上传,单文件的上传就不说了,不过代码都是现成的. 效果预览:   DEMO图:     添加文件上传j ...

  5. SpringMVC国际化与文件上传

    点击阅读上一章 其实SpringMVC中的页面国际化与上一章的验证国际化基本一致. 1.对页面进行国际化 1)首先我们对Spring配置文件中添加国际化bean配置 <!-- 注册国际化信息,必 ...

  6. SpringMVC框架06——文件上传与下载

    1.文件上传 Spring MVC框架的文件上传是基于commons-fileupload组件的文件上传,只不过Spring MVC框架在原有文件上传组件上做了进一步封装,简化了文件上传的代码实现. ...

  7. SpringMVC高速实现文件上传功能

    SpringMVC为我们封装了上传文件的功能,如今就试用一下 须要的jar包 我用的是Maven项目,就不须要到处下载Jar包了 SpringMVC的搭建 首先要在applicationContext ...

  8. SpringMVC使用MultipartFile文件上传,多文件上传,带参数上传

    一.配置SpringMVC 二.单文件与多文件上传 三.多文件上传 四.带参数上传 一.配置SpringMVC 在spring.xml中配置: <!-- springmvc文件上传需要配置的节点 ...

  9. ASP.NET MVC5+EF6+EasyUI 后台管理系统(56)-插件---单文件上传与easyui使用fancybox

    系列目录 https://yunpan.cn/cZVeSJ33XSHKZ  访问密码 0fc2 今天整合lightbox插件Fancybox1.3.4,发现1.3.4版本太老了.而目前easyui 1 ...

随机推荐

  1. UINavigationBar 总结

    一.某个 ViewController 出现隐藏 NavigationBar - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear ...

  2. 树莓派wiringPi库详解

    wiringPi是一个很棒的树莓派IO控制库,使用C语言开发,提供了丰富的接口:GPIO控制,中断,多线程,等等.java 的pi4j项目也是基于wiringPi的,我最近也在看源代码,到时候整理好了 ...

  3. open-falcon 安装

    openfalcon 规划 拓扑图 transfer 配置文件 [root@openfalcon_transfer1 transfer]# cat cfg.json { "debug&quo ...

  4. selenium and win32api

    I:Web 测试 selenium-2.45.0 python 2.7.8 win32api http://sourceforge.net/projects/pywin32/files/pywin32 ...

  5. 图割Graph-Cut的最大流实现

    利用最大流标号法求解最大流,详见代码: Version:未加头尾节点版: 缺点:havn't take nodes' pixels into consideration /************** ...

  6. wampserver---------如何让局域网其他电脑访问我的apache

    wamp版本:wampserver2.5-Apache-2.4.9-Mysql-5.6.17-php5.5.12-64b 修改方式,看图:修改完记得重启.

  7. Windows下pry安装和配置

    Windows下pry安装和配置 pry是一个增强型的交互式命令行工具,比irb强大. 有自动完成功能,自动缩进,有颜色.有更强大的调试功能. pry 安装很简单. 在终端输入: gem instal ...

  8. 使用规则引擎Drools计算圆周率PI

    实际上是使用规则引擎能够更新工作内存区重新匹配规则实现迭代功能. 使用了策略模式实现. <规则引擎与RETE算法介绍> PPT : http://files.cnblogs.com/lov ...

  9. (转) Eclipse连接MySQL数据库(傻瓜篇)

    Eclipse连接MySQL数据库(傻瓜篇) 原帖地址: http://www.cnblogs.com/fnng/archive/2011/07/18/2110023.html Posted on 2 ...

  10. noi 2728 摘花生

    题目链接: 很像上一题,加上自己本身,选最优值. http://noi.openjudge.cn/ch0206/2728/ http://paste.ubuntu.com/23402493/