• spring mvc实现文件上传与下载


    文件上传

    在pom.xml中引入spring mvc以及commons-fileupload的相关jar

            <!-- spring mvc -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>4.3.13.RELEASE</version>
            </dependency>
            
            <!-- 文件上传与下载 -->
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>1.3.3</version>
            </dependency>

    在springmvc.xml中加入文件上传的相关配置

       <bean id="multipartResolver"  
            class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
            <!-- 上传文件大小上限,单位为字节(10MB) -->
            <property name="maxUploadSize">  
                <value>10485760</value>  
            </property>  
            <!-- 请求的编码格式,必须和jSP的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1 -->
            <property name="defaultEncoding">
                <value>UTF-8</value>
            </property>
        </bean>

    在jsp文件中加入form表单

         <form action="upload" enctype="multipart/form-data" method="post">
            <table>
                <tr>
                    <td>文件描述:</td>
                    <td><input type="text" name="description"></td>
                </tr>
                <tr>
                    <td>请选择文件:</td>
                    <td><input type="file" name="file"></td>
                </tr>
                <tr>
                    <td><input type="submit" value="上传"></td>
                </tr>
            </table>
        </form>
    

    添加文件上传的方法

    //上传文件会自动绑定到MultipartFile中
         @RequestMapping(value="/upload",method=RequestMethod.POST)
         public String upload(HttpServletRequest request,
                @RequestParam("description") String description,
                @RequestParam("file") MultipartFile file) throws Exception {
    
            //如果文件不为空,写入上传路径
            if(!file.isEmpty()) {
                //上传文件路径
                String path = request.getServletContext().getRealPath("/file/");
                //上传文件名
                String filename = file.getOriginalFilename();
                File filepath = new File(path,filename);
                //判断路径是否存在,如果不存在就创建一个
                if (!filepath.getParentFile().exists()) { 
                    filepath.getParentFile().mkdirs();
                }
                //将上传文件保存到一个目标文件当中
                file.transferTo(new File(path + File.separator + filename));
             request.setAttribute("uploadfileName", filename);
                return "success";
            } else {
                return "error";
            }
    
         }
    

      

    文件下载

    在页面给一个超链接,其href属性值就是要下载文件的文件名

    SpringMVC提供了一个ResponseEntity类型,使用它可以很方便地定义返回的HttpHeaders和HttpStatus

    <a href="download?filename=${uploadfileName}"> ${uploadfileName} </a>

     

         @RequestMapping(value="/download")
         public ResponseEntity<byte[]> download(HttpServletRequest request,
                 @RequestParam("filename") String filename)throws Exception {
            //下载文件路径
            String path = request.getServletContext().getRealPath("/file/");
            File file = new File(path + File.separator + filename);
            HttpHeaders headers = new HttpHeaders();  
            //下载显示的文件名,解决中文名称乱码问题  
            String downloadFielName = new String(filename.getBytes("UTF-8"),"iso-8859-1");
            //通知浏览器以attachment(下载方式)打开
            headers.setContentDispositionFormData("attachment", downloadFielName); 
            //application/octet-stream : 二进制流数据(最常见的文件下载)。
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),    
                    headers, HttpStatus.CREATED);  
         }
    

      

  • 相关阅读:
    POJ1704 Georgia and Bob
    BZOJ1299 巧克力棒
    IPSec
    GRE协议
    L2TP协议
    AAA及Radius
    网络安全概论
    路由策略与引入
    BGP协议
    路由协议
  • 原文地址:https://www.cnblogs.com/xiaoQ0725/p/8080425.html
Copyright © 2020-2023  润新知