从springBoot框架服务器上下载文件 自定义一个启动器

在springboot框架中下载服务器存储的图片:

1)springboot默认访问放行的目录只有static,在static目录下存放图片资源

2)编译后的static目录中有一个1.png

2.5)编写控制器:

java 复制代码
@Controller
//@RequestMapping("/upload")
public class UploadController {
    
    @RequestMapping("/download")
    public void download( String imgName,HttpServletRequest request,HttpServletResponse response) throws IOException {
        response.setContentType("image/png"); // 根据实际文件类型设置
        response.setHeader("Content-Disposition", "attachment; filename="+imgName);
        try {
            URL url = new URL("http://localhost:8080/upload/"+imgName);
            /*将网络资源地址传给,即赋值给url*/
            /*此为联系获得网络资源的固定格式用法,以便后面的in变量获得url截取网络资源的输入流*/
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setRequestMethod("GET");

            DataInputStream in = new DataInputStream(connection.getInputStream());
            /*此处也可用BufferedInputStream与BufferedOutputStream*/
            DataOutputStream out = new DataOutputStream(response.getOutputStream());

            /*将参数savePath,即将截取的图片的存储在本地地址赋值给out输出流所指定的地址*/
            byte[] buffer = new byte[4096];
            int count = 0;
            /*将输入流以字节的形式读取并写入buffer中*/
            while ((count = in.read(buffer)) > 0) {
                out.write(buffer, 0, count);
            }

            out.close();/*后面三行为关闭输入输出流以及网络资源的固定格式*/
            in.close();
            connection.disconnect();

        } catch (Exception e) {
//            System.out.println(e + fileUrl + savePath);
//            return null;
            System.out.println("上传异常"+e.getMessage());
            System.out.println(e);
        }
    }

}

3)访问控制器

自定义一个启动器:

1)创建一个Maven项目,在pom.xml文件中:

一:所有启动器配置类的创建使用spring-boot-autoconfigure实现

二:spring-boot-configuration-processor找到boot的配置文件实现映射.

java 复制代码
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.11</version>
    </parent>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

2)Maven目录结构:

DiyProperties用来映射配置文件

DiyBean用来装配diyProperties对象

DiyConfigration是一个配置类容器,存放bean对象.



DiyProperties:

java 复制代码
package com.xja.start;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * @author rk
 * @description: TODO
 * @date 2024/8/13 21:19
 */
@ConfigurationProperties(
        prefix = "mybatis"
)
public class DiyProperties {
    private Integer param;
    private String docuration;

    public DiyProperties() {
    }

    public DiyProperties(Integer param, String docuration) {
        this.param = param;
        this.docuration = docuration;
    }

    public Integer getParam() {
        return param;
    }

    public void setParam(Integer param) {
        this.param = param;
    }

    public String getDocuration() {
        return docuration;
    }

    public void setDocuration(String docuration) {
        this.docuration = docuration;
    }
}


DiyBean:

java 复制代码
package com.xja.bean;

import com.xja.start.DiyProperties;

/**
 * @author rk
 * @description: TODO
 * @date 2024/8/13 22:34
 */
public class DiyBean {
    private DiyProperties properties;

    public DiyBean(DiyProperties properties) {
        this.properties = properties;
    }

    public DiyProperties getProperties() {
        return properties;
    }

    public void setProperties(DiyProperties properties) {
        this.properties = properties;
    }
}


DiyConfigration:

java 复制代码
package com.xja.config;

import com.xja.bean.DiyBean;
import com.xja.start.DiyProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author rk
 * @description: TODO
 * @date 2024/8/13 22:29
 */
@Configuration
@EnableConfigurationProperties({DiyProperties.class})
public class DiyConfigration {
//    @Autowired
//    private DiyBean diyBean;

    @Bean
    public DiyBean properties(DiyProperties diyProperties){
        return new DiyBean(diyProperties);
    }


}

spring.factories:

本Maven项目的配置文件,这个文件给springboot框架看的,boot框架会根据该配置文件实现对配置类的装配.

java 复制代码
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.xja.config.DiyConfigration

3)安装并在其他项目启用启动器:

安装:

启用:

1)新建maven项目启用该坐标
2)配置application.yml
3)入口不要忘写了
4)启动器,启动!
相关推荐
小中12341 分钟前
异步请求的性能提升
前端
程序员卷卷狗2 分钟前
JVM实战:从内存模型到性能调优的全链路剖析
java·jvm·后端·性能优化·架构
Android-Flutter3 分钟前
kotlin - 正则表达式,识别年月日
java·kotlin
得物技术3 分钟前
线程池ThreadPoolExecutor源码深度解析|得物技术
java·编译器·dns
我是天龙_绍3 分钟前
渐变层生成器——直接用鼠标拖拽就可以调整渐变层的各种参数,然后可以导出为svg格式
前端
道可到5 分钟前
直接可以拿来的面经 | 从JDK 8到JDK 21:一次团队升级的实战经验与价值复盘
java·面试·架构
world-wide-wait31 分钟前
python高级04——网络编程
linux·服务器·网络
我是天龙_绍31 分钟前
Easing 曲线 easings.net
前端
知识分享小能手34 分钟前
微信小程序入门学习教程,从入门到精通,电影之家小程序项目知识点详解 (17)
前端·javascript·学习·微信小程序·小程序·前端框架·vue
迎風吹頭髮35 分钟前
Linux内核架构浅谈26-Linux实时进程调度:优先级反转与解决方案
linux·服务器·架构