Springboot文件上传到Linux服务器

spring-boot项目中文件上传,使用传统的方法是不可以的。需要通过映射获取文件,在页面显示或者是下载
1.yml配置

file:
  #服务器地址
  uploadurl: "/upload/file/"      // 最后一个'/' 不能
  #本地地址
  #localurl: "D:/upload/resource/file/"

注意:地址最后面的’/’ 一定不要忘记!!
2.文件上传

 @Value("${file.uploadUrl}")
 public String path;

@PostMapping("/test")
    public void test(MultipartFile file,HttpServletRequest request) throws IOException {
        //获取文件名
        String oldName = file.getOriginalFilename();
        String suffix = oldName.substring(oldName.lastIndexOf(".") + 1);
        String newName = UUID.randomUUID().toString() +"."+ suffix;
        File file1 = new File(path,newName);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        // 保存文件
        file.transferTo(file1);
        
    }

3.配置路劲映射

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("/file/**") // 访问路劲
        	.addResourceLocations("file:"+fileUrl); // 资源真实路径
    }
}

注意:配置本地上传地址或者服务器地址,springboot项目可以通过映射获取文件,从而页面显示

4.访问:项目访问路径+配置的映射访问路径
在这里插入图片描述

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
你可以使用Spring Boot来实现文件上传Linux服务器的功能。下面是一个简单的示例: 1. 首先,确保你的Spring Boot项目已经配置好了文件上传的依赖。你可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 2. 在你的Controller中创建一个处理文件上传请求的接口。你可以使用`MultipartFile`类型的参数来接收上传的文件,并使用`TransferTo`方法将文件保存到指定的目录中。例如: ```java import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; @RestController public class FileUploadController { @PostMapping("/upload") public String uploadFile(@RequestParam("file") MultipartFile file) { if (file.isEmpty()) { return "请选择要上传的文件"; } try { String uploadDir = "/path/to/your/upload/directory"; File dest = new File(uploadDir + "/" + file.getOriginalFilename()); file.transferTo(dest); return "文件上传成功"; } catch (IOException e) { e.printStackTrace(); return "文件上传失败"; } } } ``` 3. 在你的application.properties或application.yml文件中配置上传文件的临时路径。例如: ```yaml spring.servlet.multipart.enabled=true spring.servlet.multipart.file-size-threshold=2KB spring.servlet.multipart.max-file-size=200MB spring.servlet.multipart.max-request-size=215MB spring.servlet.multipart.location=/tmp ``` 4. 启动你的Spring Boot应用程序,并使用POST请求将文件上传到`/upload`接口。你可以使用Postman或类似的工具来测试。 请注意,上述示例中的`/path/to/your/upload/directory`应替换为你希望保存上传文件Linux服务器上的目录路径。 希望这个简单示例对你有所帮助!如果你有任何更多的问题,请随时提问。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值