Springboot上传文件到Linux服务器

jar打包方式不支持将文件动态写入文件,这时需要通过映射的方式将文件上传到映射某一个文件夹,通过映射获取文件,在页面显示。

1.yml配置

  • 配置本地上传地址或者服务器地址,springboot项目可以通过映射获取文件,从而页面显示
  • 注意:这里配置的地址一定要加一个"/"在最后面!!!!
file:
  #服务器地址
  uploadurl: "/u01/upload/images/"
  #本地地址
  #localurl: "D:/springbootFile/upload/images/"

2.上传方法

 

  • 获取配置文件中配置的文件存储路径,将图片存储到本地或者服务器,页面通过映射获取。
      @Value("${file.uploadurl}")
    private String uploadPath;

 @PostMapping("/fileUpload")
    @ApiOperation(value = "上传下载图片")
    public JsonResult filesUpload(@RequestParam("filesName") MultipartFile file,
                                             @RequestParam("mmsi") String mmsi) throws IOException {

        JsonResult result = new JsonResult();
        //如果文件夹不存在,创建
        File fileP = new File(uploadPath);

        if (!fileP.isDirectory()) {
            //递归生成文件夹
            fileP.mkdirs();
        }
        String fileName = "";
        if(file.getOriginalFilename().endsWith(".jpg")){
            fileName =String.format("%s.jpg",System.currentTimeMillis());
        }else if(file.getOriginalFilename().endsWith(".png")){
            fileName =String.format("%s.jpg",System.currentTimeMillis());
        }else if(file.getOriginalFilename().endsWith(".jpeg")){
            fileName =String.format("%s.jpeg",System.currentTimeMillis());
        }else if(file.getOriginalFilename().endsWith(".bmp")){
            fileName =String.format("%s.bmp",System.currentTimeMillis());
        }else{
            result.setSuccess(false);
            result.setCode("0");
            result.setMessage("图片格式不正确!,使用.jpg/.png/.bpm/.jpeg后缀的图片");
            return result;
        }
        file.transferTo(new File(fileP,fileName));
        //数据库存入地址
        cxShipDetailService.insertShipPic(mmsi,uploadPath+fileName,fileName);
        result.setSuccess(true);
        result.setCode("1");
        result.setMessage("上传图片成功!");
        return result;
    }

上传功能就到此结束了。那么肯定会问,上传了怎么获取图片呢?很简单,通过地址映射就可以获取了。

3.配置类

 

  • 配置映射路径,例如:页面请求的图片路径为(默认到static目录下):images/111.jpg,static目录下没有该目录文件,将通过映射的imges到本地或者服务器的存储中获取。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class FileConfig   implements WebMvcConfigurer {

    @Value("${file.uploadurl}")
    private String  fileUrl;

    public void addResourceHandlers(ResourceHandlerRegistry register){
        register.addResourceHandler("/images/**").addResourceLocations("file:"+fileUrl);
    }
}

方式二:除了配置类还有另一种方式,那就是直接在配置文件配置spring的资源文件地址:

spring:
  mvc: 
    static-path-pattern: /image/**
  resources:
    #本地地址
    static-locations: file:D://test/
    #linux服务器地址
   #static-locations: file:/u01/upload/images/

但是通过方式二有弊端,如果配置了swagger你会发现不能打开swagger了,是因为默认资源位置已被映射,无法找到, 所以需要通过配置类指定资源位置


import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class WebMvcConfig implements WebMvcConfigurer  {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

    }

}

建议直接使用第一种方式!

效果:这里的imags对应配置类里面的获取地址。

  • 12
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
### 回答1: 1. 首先,需要在Linux服务器上安装Java环境,可以通过以下命令安装: ``` sudo apt-get update sudo apt-get install openjdk-8-jdk ``` 2. 然后,将Spring Boot应用程序打包成可执行的JAR文件,可以使用Maven或Gradle构建工具来完成此操作。 3. 将JAR文件Linux服务器,可以使用scp命令将文件从本地计算机复制到远程服务器: ``` scp /path/to/your/app.jar user@server:/path/to/remote/directory ``` 4. 在Linux服务器上运行Spring Boot应用程序,可以使用以下命令: ``` java -jar /path/to/your/app.jar ``` 如果需要在后台运行应用程序,可以使用nohup命令: ``` nohup java -jar /path/to/your/app.jar & ``` 5. 最后,可以使用浏览器访问应用程序的URL,例如http://localhost:808,以确保应用程序已成功部署到Linux服务器上。 ### 回答2: SpringBoot是一个快速开发应用程序的框架,它提供了自动配置功能和开箱即用的注解,使得开发者能够更快速地完成应用程序的开发和部署。那么如何将SpringBoot应用程序部署到Linux服务器上呢?本文将为大家提供一些关键步骤。 1.安装Java SpringBoot应用程序是基于Java的,因此首先需要在服务器上安装Java环境。我们可以使用命令"sudo apt-get install openjdk-8-jdk"来安装OpenJDK 8。 2.上SpringBoot应用程序 将SpringBoot应用程序打包成jar包,并将其上服务器。可以使用FTP或SCP等工具上。 3.创建启动脚本 创建一个启动脚本来启动SpringBoot应用程序。启动脚本通常包括Java命令、启动参数、日志配置等。例如: ``` #!/bin/bash #定义应用程序目录和jar包名称 APP_HOME=/home/myapp APP_NAME=myapp.jar # java命令 JVM_OPTS="-Xms256m -Xmx256m" # 启动命令 nohup java $JVM_OPTS -jar $APP_HOME/$APP_NAME --spring.profiles.active=prod > $APP_HOME/myapp.log 2>&1 & ``` 4.部署启动脚本 将启动脚本部署到服务器的合适位置,并赋予脚本执行权限。例如: ``` sudo chmod +x /home/myapp/myapp.sh ``` 5.启动应用程序 执行启动脚本来启动SpringBoot应用程序。例如: ``` sudo /home/myapp/myapp.sh start ``` 6.检查应用程序状态 使用命令"ps -ef | grep myapp"可以查看SpringBoot应用程序的进程状态。如果应用程序正在运行,则应该看到与应用程序相关的进程。 至此,我们已经成功将SpringBoot应用程序部署到Linux服务器上。但是,在实际生产环境中,还需要考虑一些其他因素,例如应用程序的高可用性、负载均衡等。因此,在进行实际的生产环境部署前,需要对具体情况进行详细的规划和测试。 ### 回答3: Spring Boot 是一款开源的 Java 开发框架,可以快速构建高效稳定的 Web 应用程序,适用于大型企业级应用的开发。部署 Spring Boot 应用程序到 Linux 服务器时,需要掌握以下几个关键点。 首先,需要在 Linux 服务器上安装 JDK,Spring Boot 应用程序需要运行在 JDK 环境下。可以通过运行 java -version 命令来确保 JDK 是否已经安装成功。 然后,需要将 Spring Boot 应用程序的 jar 包上Linux 服务器上,并在服务器上创建一个目录来存储应用程序的配置文件。可以使用 scp 命令将 jar 包上服务器,例如: scp target/XXX.jar user@xxx.xxx.xxx.xxx:/home/user/app/ 接着,可以使用命令 nohup java -jar XXX.jar --spring.config.location=/home/user/app/application.yml & 来运行 Spring Boot 应用程序。其中,--spring.config.location 参数指定了应用程序的配置文件路径。 此外,为了确保 Spring Boot 应用程序在后台持续运行,可以将其包装成一个 Linux 服务,并配置自启动。例如,在 /etc/systemd/system/ 目录下创建一个名为 myapp.service 的服务文件,内容如下: [Unit] Description=Spring Boot Application [Service] User=user Group=user ExecStart=/usr/bin/java -jar /home/user/app/XXX.jar --spring.config.location=/home/user/app/application.yml SuccessExitStatus=143 [Install] WantedBy=multi-user.target 在服务文件中,我们指定了应用程序的 jar 包路径和配置文件路径,以及启动参数。然后,使用 systemctl 命令启动服务,并设置为开机自启动。 最后,需要配置防火墙,将对应的端口开放,以确保 Spring Boot 应用程序可以被外部访问。可以使用 firewall-cmd 命令进行配置,例如: firewall-cmd --add-port=8080/tcp --permanent firewall-cmd --reload 这样,就可以在 Linux 服务器上成功部署 Spring Boot 应用程序了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值