两种上传方式都可以
依赖pom.xml

<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.4</version>
        </dependency>

 Spring bean配置

<!--文件上传id必须=multipartResolver-->
    <bean >
        <!--请求的编码格式,必须和jsp的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1-->
        <property name="defaultEncoding" value="utf-8"/>
        <!--上传文件大小上限,单位为字节10485760=10M-->
        <property name="maxUploadSize" value="10485760"/>
        <property name="maxInMemorySize" value="40960"/>
    </bean>

 代码

@PostMapping("/article/upload1Handler")
    public String upload1Handler(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {
        String uploadFileName = file.getOriginalFilename();
        if ("".equals(uploadFileName)){
            return "redirect:/article/index";
        }
        System.out.println("上传文件名:" + uploadFileName);
        String path = request.getServletContext().getRealPath("/upload");
        File realPath = new File(path);
        if (!realPath.exists()) {
            realPath.mkdir();
        }
        System.out.println("上传文件地址:" + realPath);
        InputStream is = file.getInputStream();//文件输入流
        BufferedInputStream bis = new BufferedInputStream(is);
        OutputStream os = new FileOutputStream(new File(realPath,uploadFileName));//文件输出流
        BufferedOutputStream bos = new BufferedOutputStream(os);
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = is.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bis.close();
        bos.close();
        return "redirect:/article/index";
    }
    @PostMapping("/article/upload2Handler")
    public String upload2Handler(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {
        String path = request.getServletContext().getRealPath("/upload");
        File realPath = new File(path);
        if (!realPath.exists()) {
            realPath.mkdir();
        }
        System.out.println("上传文件地址:" + realPath);
        //通过CommonsMultipartFile的方法直接写文件
        file.transferTo(new File(realPath + "/" + file.getOriginalFilename()));
        return "redirect:/article/index";
    }

 视图

<form action="/article/upload2Handler" method="post" enctype="multipart/form-data">
    <input type="file" name="file"/>
    <button type="submit">Submit</button>
</form>

 下载文件:

@PostMapping("/article/downHandler")
    public String downHandler(HttpServletResponse response, HttpServletRequest request) throws IOException {
        String path = request.getServletContext().getRealPath("/upload");
        String fileName = "1.png";
        response.reset(); // 非常重要
        response.setCharacterEncoding("UTF-8");
        response.setContentType("multipart/form-data");//二进制传输数据
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
        File file = new File(path, fileName);
        InputStream is = new FileInputStream(file);
        OutputStream os = response.getOutputStream();

        BufferedInputStream bis = new BufferedInputStream(is);
        BufferedOutputStream bos = new BufferedOutputStream(os);
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = is.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bis.close();
        bos.close();
        return "redirect:/article/index";
    }

 下载文件的几种方式:https://blog.csdn.net/longzhongxiaoniao/article/details/89295891

相关文章:

  • 2021-12-12
  • 2022-12-23
  • 2021-10-03
  • 2022-02-12
  • 2021-07-25
  • 2021-10-15
  • 2021-12-11
  • 2021-07-11
猜你喜欢
  • 2022-12-23
  • 2021-09-18
  • 2022-01-02
  • 2021-05-23
  • 2021-11-30
  • 2021-10-27
相关资源
相似解决方案