首页 > 其他分享 >SpringBoot 文件上传及回显

SpringBoot 文件上传及回显

时间:2023-11-28 23:57:55浏览次数:32  
标签:文件大小 outputStream SpringBoot 回显 文件 上传 file data

文件上传/回显

SpringBoot默认tomcat上传文件大小问题(默认大小不能超过1MB)

 /**
     * 文件上传
     *
     * @param file
     * @throws IOException
     */
    @PostMapping("upload")
    @ApiOperation("文件上传")
    public void upload(@RequestParam("file") Part file) throws IOException {

        String data = "";
        LocalDateTime now = LocalDateTime.now();
        data += now.getYear();
        data += now.getMonthValue();
        data += now.getDayOfYear();
        log.info("文件上传的时间:{}", data);

        File folder = new File(PATH + data);
        if (!folder.exists() && !folder.isDirectory()) {
            folder.mkdir();
        }


        String fileName = Paths.get(file.getSubmittedFileName()).getFileName().toString();
        InputStream inputStream = file.getInputStream();
        OutputStream outputStream = new FileOutputStream(PATH + data + "\\" + fileName);
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
        outputStream.close();
        inputStream.close();
    }

    /**
     * 文件回显、下载
     *
     * @param data
     * @param name
     * @param httpServletResponse
     * @throws IOException
     */
    @GetMapping("{data}/{file:.+}")
    public void getFile(@PathVariable("data") String data, @PathVariable("file") String name, HttpServletResponse httpServletResponse) throws IOException {
        ServletOutputStream outputStream = httpServletResponse.getOutputStream();
        byte[] bytes = Files.readAllBytes(Paths.get(PATH + data + "\\" + name));
        outputStream.write(bytes);
    }

这里注意,在tomcat中默认文件上传限制大小为1M=1048576字节,如果上传的文件超过了这个限制,就会报错。

解决办法:在web.xml中配置上传文件大小限制

<web-app>
    <!-- 上传文件大小限制 -->
    <multipart-config>
        <max-file-size>102400000</max-file-size>
        <max-request-size>102400000</max-request-size>
    </multipart-config>
</web-app>

注意:上传文件大小限制为102400000字节,即100M

注意:上传文件大小限制为102400000字节,即`10

application.yml

spring:
  servlet:
    multipart:
      max-file-size: 100MB #单个文件大小
      max-request-size: 200MB  # 总文件大小(允许存储文件的文件夹大小)

标签:文件大小,outputStream,SpringBoot,回显,文件,上传,file,data
From: https://www.cnblogs.com/tobycold/p/17863480.html

相关文章

  • C:\Users\17482\Desktop\ERP——test1\SpringBoot-ERP-master\src\main\java
    这个错误表明在你的Java类文件UserImp.java中,找不到MyBatis的注解包org.apache.ibatis.annotations。这个包中包含了MyBatis的注解,比如@Select、@Insert等。首先,请确保你的项目正确引入了MyBatis的依赖。在你的pom.xml文件中应该包含类似以下的依赖配置:<dependency......
  • springboot 自定义响应体大小测试接口
    @ResponseBody@RequestMapping("/def/response/body/service")publicStringBuilderdefResponseBodyService(@RequestParam(name="count")Integercount,HttpServletRequestHttpRequest)throwsInterruptedException{  StringbaseStr="0......
  • 基于springboot的课程作业管理系统-计算机毕业设计源码+LW文档
    一、 研究目的和意义当今时代是飞速发展的信息时代。在各行各业中离不开信息处理,这正是计算机被广泛应用于信息管理系统的环境。计算机的最大好处在于利用它能够进行信息管理。使用计算机进行信息控制,不仅提高了工作效率,而且大大的提高了其安全性。尤其对于复杂的信息管理,计算机能......
  • 基于springboot的社区团购系统-计算机毕业设计源码+LW文档
    1、立论依据(课题来源、选题依据和背景情况、课题研究目的、理论意义和实际应用价值)(1)课题来源、选题依据和背景情况 本课题来自于自拟项目。 近年来,全球经济的高速发展,在一定程度上,促进了互联网技术的发展,信息化管理行业在生活中占据着越来越重要的地位,使得人们的生活方式发......
  • SpringBoot-跨域问题
    SpringBoot解决跨域问题为什么会出现跨域问题什么是跨域跨域:指的是浏览器不能执行其他网站的脚本。它是由浏览器的同源策略造成的,是浏览器对javascript施加的安全限制。例如:a页面想获取b页面资源,如果a、b页面的协议、域名、端口、子域名不同,所进行的访问行动都是跨域的,而浏览......
  • springboot集成springsecurity
    转载自:www.javaman.cn1、整合springsecurity添加pom.xml<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>2、springsecurity认证授权......
  • springboot集成springsecurity
    转载自:www.javaman.cn1、整合springsecurity添加pom.xml<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>2、springsecurity认证授权流程认证管理......
  • springboot010大学生入学审核系统的设计与实现-计算机毕业设计源码+LW文档
    研究背景如今,互联网高速发展,网络遍布全球,通过互联网发布的消息能快而方便的传播到世界每个角落,并且互联网上能传播的信息也很广,比如文字、图片、声音、视频等[1]。互联网成了信息传播的主要途径,社会上各种各样的信息都想尽办法通过互联网进行传播,互联网对社会产生的影响越来越大[2......
  • elementUI + Spring上传文件
    elementUI+Spring上传文件表单文件上传【elementUI+Spring报错解决方案】Requiredrequestpart‘***‘isnotpresent表单上传文件时需要去除@RequestBody前端文件上传doApprove(){const_this=this//创建表单对象letformData=newForm......
  • SpringBoot 实现 PDF 添加水印,我有 5 种实现方案
    Part1简介PDF(PortableDocumentFormat,便携式文档格式)是一种流行的文件格式,它可以在多个操作系统和应用程序中进行查看和打印。在某些情况下,我们需要对PDF文件添加水印,以使其更具有辨识度或者保护其版权。本文将介绍如何使用SpringBoot来实现PDF添加水印的方式。1使用Apach......