springmvc上传下载

上传

参考

  1. 导包
        <!--springmvc 上传-->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
    
  2. 配置文件
    <!--文件上传-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--限制文件大小-->
        <property name="maxUploadSize" value="10485760"></property>
        <property name="defaultEncoding" value="UTF-8"></property>
    </bean>
    
  3. jsp页面
    <html>
    <head>
        <title>上传</title>
    </head>
    <body>
        <%--使用post请求,enctype="multipart/form-data"--%>
    <form action="/upload.do" method="post" enctype="multipart/form-data">
        <label>头像</label><input type="file" name="file"><br>
        <label>用户名</label><input type="text" value="" placeholder="用户名" name="username"><br>
        <label>密码</label><input type="password" value="" placeholder="密码" name="password"><br>
        <input type="submit" value="提交">
    </form>
    </body>
    </html>
    
  4. controller层
    @RequestMapping(value = "/upload.do")
    @ResponseBody
    public void testUpload(
        HttpServletRequest request,
        HttpServletResponse response,
        User user,
        @RequestParam(value = "file") MultipartFile file)
        throws IOException {
        // 获取用户信息
        System.out.println("用户名");
        System.out.println(user.getUsername());
        System.out.println("密码");
        System.out.println(user.getPassword());
        // 获取文件名
        String filename = file.getOriginalFilename();
        System.out.println(filename);
        // 获取绝对路径
        String upload_Path = request.getRealPath("/upload");
        // 路径处理
        String file_path = upload_Path + "//" + UUID.randomUUID() + filename;
        // 判断路径是否存在如果没有就创建
        File file1 = new File(file_path);
        if (!file1.getParentFile().exists()) {
        file1.getParentFile().mkdirs();
        }
        // 将文件file里的内容复制到file1里
        file.transferTo(file1);
    }
    

下载

  1. jsp页面
     <html>
     <head>
         <title>Title</title>
     </head>
     <body>
     <a href="download.do?filename=f12c1a77-158f-466f-b272-33144f668de3_4Ib-a8g9aA.jpg">下载</a>
     </body>
     </html> 
    
  2. controller层
        @RequestMapping("/download")
    public ResponseEntity<byte[]> downloadTest(
        HttpServletRequest request, @RequestParam("filename") String filename, Model model)
        throws IOException {
        filename = new String(filename.getBytes("ISO-8859-1"), "UTF-8");
        // 文件下载路径
        String path = request.getRealPath("/upload/");
        System.out.println("path:" + path);
        File file = new File(path + File.separator + filename);
        HttpHeaders httpHeaders = new HttpHeaders();
        // 解决文件名的乱码问题
        String downloadFileName = new String(filename.getBytes("UTF-8"), "ISO-8859-1");
        System.out.println("downloadFileName" + downloadFileName);
        // 以attachment方式(下载)打开文件
        httpHeaders.setContentDispositionFormData("attachment", downloadFileName);
        // 二进制流数据,常见的文件下载
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(
            FileUtils.readFileToByteArray(file), httpHeaders, HttpStatus.CREATED);
    }
    
原文地址:https://www.cnblogs.com/JaminYe/p/10631602.html