zoukankan      html  css  js  c++  java
  • Springmvc文件下载和上传

     @RequestMapping(value = "/downLoadFile.do") 

      public ResponseEntity<byte[]> download(@RequestParam("name") String name,       

       @RequestParam("filePath") String path) throws IOException {   

       File file = new File(path);     

     HttpHeaders headers = new HttpHeaders(); 

         String fileName = new String(name.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);   

    }

    Html页面,这里只是简单的填写文件名和文件路径,用form表单提交到后台,然后后台会控制response在页面弹出保存文件路径的选择框,当然了,这里的后台我没有做什么处理,如果文件不存在是会报错的,可以进行相应的处理:

    文件下载:

       </br> </br>   

    <form action="./downLoadFile.do"style="border:1px solid grey;auto;" method="post">         

     文件名:<input type="text" name="name"/></br></br>       

       文件路径:<input type="text" name="filePath"/></br></br>       

       <input type="submit" value="确认下载"/> 

     </form>


    页面视图如下:

     如果文件不存在,报错如下:

     SpringMVC文件上传:

    1.导入依赖:

    commons-io

    commons-fileupload

    <dependency>

          <groupId>commons-io</groupId>

          <artifactId>commons-io</artifactId>

          <version>2.4</version>

        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->

        <dependency>

          <groupId>commons-fileupload</groupId>

          <artifactId>commons-fileupload</artifactId>

          <version>1.3.1</version>

        </dependency>

    3.构建文件上传控制器

    @RequestMapping("/fileUpload")

        /**

         * MultipartFile 选择的文件

         */

        public String fileupload(HttpSession session,MultipartFile file,String author) throws IOException {

            System.out.println("作者:"+author);

            System.out.println(file);

            /**

             * 如何处理文件

             */

            if(!file.isEmpty()){

                //获取文件名称

                String fileName=file.getName();

                //获取到需要上传的路径

                String realPath = session.getServletContext().getRealPath("/WEB-INF/upload");

                //创建文件对象

                File uploadfile=new File(realPath+"\"+fileName);

                //如何上传文件

                file.transferTo(uploadfile);

            }

            return "index";

        }

    4.注册文件上传解析器

    <!--文件上传解析器-->

        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

            <!--编码-->

            <property name="defaultEncoding" value="UTF-8"/>

            <property name="maxUploadSize" value="500000"/>

        </bean>

    /**     * 文件下载,需要文件名和文件地址     *     * @author:tuzongxun     * @Title: download     * @param@param name     * @param@param path     * @param@return     * @param@throws IOException     * @returnResponseEntity<byte[]>     * @date Apr 28,2016 1:21:32 PM     * @throws     */    @RequestMapping(value = "/downLoadFile.do")    public ResponseEntity<byte[]> download(@RequestParam("name") String name,           @RequestParam("filePath") String path) throws IOException {       File file = new File(path);       HttpHeaders headers = new HttpHeaders();       String fileName = new String(name.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);    }Html页面,这里只是简单的填写文件名和文件路径,用form表单提交到后台,然后后台会控制response在页面弹出保存文件路径的选择框,当然了,这里的后台我没有做什么处理,如果文件不存在是会报错的,可以进行相应的处理:
    文件下载:   </br> </br>   <form action="./downLoadFile.do"style="border:1px solid grey;auto;" method="post">           文件名:<input type="text" name="name"/></br></br>           文件路径:<input type="text" name="filePath"/></br></br>           <input type="submit" value="确认下载"/>   </form>页面视图如下:


    如果文件不存在,报错如下:————————————————版权声明:本文为CSDN博主「涂宗勋」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/tuzongxun/article/details/51276943

  • 相关阅读:
    172. Factorial Trailing Zeroes
    96. Unique Binary Search Trees
    95. Unique Binary Search Trees II
    91. Decode Ways
    LeetCode 328 奇偶链表
    LeetCode 72 编辑距离
    LeetCode 226 翻转二叉树
    LeetCode 79单词搜索
    LeetCode 198 打家劫舍
    LeetCode 504 七进制数
  • 原文地址:https://www.cnblogs.com/wang01/p/11836280.html
Copyright © 2011-2022 走看看