java io实现文件上传下载 缓冲流性能测试

本文介绍了在Spring Boot项目中使用Java IO进行文件上传和下载的方法,包括利用MultipartFile处理文件上传,使用transferTo方法和IO流的方式。同时,文章还进行了缓冲流性能测试,建议在实际开发中采用缓冲流配合write(byte[] b)方法以获得最佳性能。
摘要由CSDN通过智能技术生成

配置文件

server.port=81
server.servlet.context-path=/augur
server.servlet.session.timeout=100
server.tomcat.uri-encoding=UTF-8

#视图解析器配置:禁用 thymeleaf 缓存
spring.thymeleaf.cache=false

#windows 文件上传路径(springboot可自定义参数,后端用@value注解接受)
fileUploadPath = E:/files/

#linux 文件上传路径
#fileUploadPath = /home/files/

# springboot2.0之后更新了配置 单个文件上传大小限制
spring.servlet.multipart.maxFileSize = 2000MB

#springboot2.0之后更新了配置 单次请求文件大小限制
spring.servlet.multipart.max-request-size = 2000MB

maven依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

<!--springboot无法直接访问templates下的页面,需要导入thymeleaf才可以访问-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

<!--  @Slf4j  日志注解使用的依赖-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
        </dependency>

    </dependencies>

 关于thymeleaf的介绍:

springboot推荐的模板引擎,主要用于编写动态页面,比jsp执行效率高。springboot只需要导入1个依赖即可整合thymeleaf,使用方便快捷,在springboot项目的resources/templates目录下创建index.html页面,写controller,使其跳转到index.html即可。   

springboot 的web项目中,static目录一般放静态资源,而templates目录一般放动态页面。static目录下的资源可以通过ip:端口/上下文路径/../静态资源文件名 直接访问。默认没有权限管理。

//    访问templates下的页面
//    方法1,字符串;如果要带数据,方法参数可以加个ModelAndView
    @GetMapping("info1")
    public String  show(){
        return "info";
    }

//    方法2,返回ModelAndView
    @GetMapping("/")
    public ModelAndView fileUpload(){
    
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值