SSM中文件上传与下载

来源:互联网 发布:淘宝卖家活动报名 编辑:程序博客网 时间:2024/06/05 18:58

单文件上传

在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);    }


下载效果
下载效果