SSM文件上传与下载

这是在原来的ssm项目基础上加的文件上传与下载,如果有不懂ssm框架集成的也可以点击这里查看,文章末尾有这篇文章的项目打包下载。

单文件上传

在spring-mvc中配置文件上传的解析器
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!--  默认编码   -->
    <property name="defaultEncoding" value="utf-8" />
    <!--  文件大小最大值  -->
    <property name="maxUploadSize" value="10485760000" />
    <!-- 内存中的最大值   -->
    <property name="maxInMemorySize" value="40960" />
</bean>
编写Controller文件:
@RequestMapping("upload")
    public ModelAndView uploadFile(MultipartFile uploadFile,HttpSession session){
        //获取上传文件名
        String filename = uploadFile.getOriginalFilename();
        //获取WebRoot下的images文件夹的绝对路径作为前半部分路径
        String leftPath = session.getServletContext().getRealPath("/images");
        //将文件的前半部分路径与文件名拼接
        File file = new File(leftPath, filename);
        try {
            uploadFile.transferTo(file);
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
        }
        ModelAndView mv = new ModelAndView();
        mv.setViewName("index");
        return mv;
    }
网页代码:
<form action="${pageContext.request.contextPath }/upload" method="post" enctype="multipart/form-data">
   <h2>文件上传</h2>
                文件:<input type="file" name="uploadFile"/><br/><br/>
      <input type="submit" value="上传"/>
</form>

到这里单文件上传就已经完成了,下面就是最后效果
上传后的结果

多文件上传

多文件上传即在原本上传的基础上,将传入参数MultipartFile uploadFile修改为MultipartFile[] uploadFile数组形式,在上传时就可以遍历这个数组来完成多文件上传

@RequestMapping("upload")
    public ModelAndView uploadFile(MultipartFile[] uploadFile,HttpSession session){
        //获取上传文件名
        for (int i = 0; i < uploadFile.length; i++) {
            String filename = uploadFile[i].getOriginalFilename();
            //获取WebRoot下的images文件夹的绝对路径作为前半部分路径
            String leftPath = session.getServletContext().getRealPath("/images");
            //将文件的前半部分路径与文件名拼接
            File file = new File(leftPath, filename);
            try {
                uploadFile[i].transferTo(file);
            } catch (IllegalStateException | IOException e) {
                e.printStackTrace();
            }
        }
        ModelAndView mv = new ModelAndView();
        mv.setViewName("index");
        return mv;
    }
jsp页面
<form action="${pageContext.request.contextPath }/upload" method="post" enctype="multipart/form-data">
   <h2>文件上传</h2>
      文件1:<input type="file" name="uploadFile"/><br/>
      文件2:<input type="file" name="uploadFile"/><br/>
      文件3:<input type="file" name="uploadFile"/><br/>
   <input type="submit" value="上传"/>
</form>

多文件上传结果

文件下载

文件下载的一些内容可以去看看对请求头的分析

@RequestMapping("/download")
    public ResponseEntity<byte[]> downFile() throws IOException{
        File file = new File("C:\\Users\\TU\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\SSM_beta1\\images\\456.png");
        HttpHeaders headers = new HttpHeaders();
        String filename = new String("helloWorld.png".getBytes("UTF-8"),"iso-8859-1");
        //设置文件名
        headers.setContentDispositionFormData("attachment", filename);
        //以文件下载的形式来输出流
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),    
                headers, HttpStatus.CREATED);
    }

下载效果
下载效果
按照国际惯例,最后贴出地址http://download.csdn.net/download/qq_31962349/9898716

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值