springboot文件上传与下载

我上传文件的目录是与项目同一级目录,也就是说在开发阶段,文件会在与src同级的目录下生成file文件,打成jar包后,会在jar包所在的目录下生成file文件
文件返回路径名称为uuid+扩展名

yaml

#文件上传路径
spring:
  servlet:
    multipart:
      #      上传文件大小设置
      enabled: true #是否启用http上传处理
      max-request-size: 1024MB #最大请求文件的大小
      max-file-size: 1024MB #设置单个文件最大长度
file:
  #虚拟路径,对外展示
  staticAccessPath: /xxx/file/**
  # 上传路径
  fileupload: /file/

Controller

@RestController
@RequestMapping("/xxxxx")
public class FileUploadController {

    @Value("${file.fileupload}")
    String fileupload;

    @ApiOperation("文件上传")
    @PostMapping("upload")
    public String upload(@RequestParam("file") MultipartFile uploadFile) {
        //获取项目所在位置
        //本地项目
        //Path path = Paths.get("");
        //String parentPathString = path.toAbsolutePath().toString();
        //System.out.println(parentPathString);
        //linux项目
        ApplicationHome ah = new ApplicationHome(getClass());
        File parentPathString =  ah.getSource().getParentFile();		
		
        //创建文件
        File file = new File(parentPathString+fileupload);
        if (!file.exists()){
         	   file.mkdirs();
        }

        //获取文件名
        String filename = uploadFile.getOriginalFilename();
        filename = filename.substring(filename.lastIndexOf("."));
        String uuid= UUID.randomUUID().toString().replace("-", "");
        filename=uuid+filename;

        try {
            //导入文件
            uploadFile.transferTo(new File(parentPathString+fileupload,filename));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileupload+filename;
    }
}

Configuration

@Configuration
public class FileUploadConfig extends WebMvcConfigurationSupport {

    @Value("${file.staticAccessPath}")
    String staticAccessPath;
    @Value("${file.fileupload}")
    String fileupload;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //获取项目所在位置
        Path path = Paths.get("");
        String parentPathString = path.toAbsolutePath().toString();
        //                          访问路径                         图片本地地址
     	registry.addResourceHandler(staticAccessPath).addResourceLocations("file:"+parentPathString+fileupload);
	//这两个是配置swagger的,如果项目使用了swagger就需要配置,否则无法使用swagger
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/","classpath:/META-INF/resources/webjars/");

        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/","classpath:/META-INF/resources/webjars/");
    }
}

pom.xml

        <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>
        <!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>

特别注意

linux上文件有权限,我们需要把你项目的当前目录的文件权限改为可写的。具体怎么改网上有很多资料,在此就不再赘述了。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狴犴ys

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值