Springmvc文件上传下载

@RequestMapping("update")
    public String update(Person per,MultipartFile file,HttpServletRequest request) throws Exception {
     String realPath = request.getSession().getServletContext().getRealPath("upload");
     String fileName = file.getOriginalFilename();//取文件名
     //解决同名问题
     fileName = UUID.randomUUID().toString().replace("-", "")+fileName.substring(fileName.lastIndexOf("."));
     File f1=new File(realPath,fileName);
     if(!f1.exists()){
         f1.mkdirs();//如果不存在则创建其目录
     }
     file.transferTo(f1);//执行上传
     per.setFilepath(fileName);//改变一下路径
        perdb.updatePerson(per);// 实现修改功能
        return "redirect:list.do";
    }

springmvc.xml的配置

    <!-- 配置视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/WEB-INF/per/"></property>
        <!-- 后缀 -->
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!-- 配置spring mvc上传图片大小,multipartResolver名不能改 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="104857600"></property>
    </bean>

下载

   @RequestMapping("download")
    public String download(HttpServletRequest request,HttpServletResponse response,Person per) throws Exception{//下载
        String filepath="//upload//"+per.getFilepath();//从upload下取图片的路径
        FileDownLoad.download(filepath, request, response);
        return null;
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述 SpringMVC 是包含在 Spring 框架中基于 Servlet API 构建的 Web 框架。在 M...
    jihe阅读 797评论 0 1
  • 各位小伙伴 今天咱们分享的就是 文件上传与下载 文件的上传 单个文件的上传 文件上传默认是采用的apache 的上...
    Java联盟阅读 2,300评论 0 1
  • 最近尝试使用OKHttp替代Volley作为网络请求框架,这肯定是要对OKHttp进行重新封装的,所有封装完毕,就...
    junjunxx阅读 4,675评论 0 13
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,837评论 18 139
  • 啤酒又叫麦酒,原料主要是大麦或小麦,公元前3000多年前就有了,是世界上历史最悠久,普及范围最广的酒精饮料。传统工...
    罗大奋阅读 541评论 1 3