Springboot实现一个简单的文件上传下载的demo

今天闲着没事儿,自己刷博客,看到很多写文件上传下载的,自己也学着搞了一个。自己在本地将文件上传,然后再下载到本地。
一、创建一个maven项目,导入依赖:

        <!--文件上传-->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.3</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

二、启动类:

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * admin模块的启动类
 */

@SpringBootApplication(scanBasePackages = "com.java.admin")
@MapperScan(basePackages = "com.java.admin.mapper")
//@EnableEurekaClient
public class AdminStart {

    public static void main(String[] args) {
        SpringApplication.run(AdminStart.class,args);
    }
}

三、配置文件:

#配置后台admin的端口号
server:
  port: 8999
#给后台admin模块设置名字
spring:
  application:
    name: hbuy-admin
  #文件上传下载
  servlet:
    multipart:
      #单个文件上传大小
      max-file-size: 200MB
      #连续上传文件大小
      max-request-size: 600MB
youku1327:
  file:
    root:
      #存储路径
      path: "E:/test/"

四、controller:

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: FileUploadController
 * @Description:   此工具可以上传单个文件,也可以上传多个文件,目前经过测试支持的文件有:图片jpg、PNG、文件、视频
 * @Version: v1.0.0
 * @Author: Fu Hao
 * @Date: 2019/12/22 0022 上午 10:26
 * Modification History:
 * Date           Author      Version     Description
 * -------------------------------------------------------------
 * 2019/12/22 0022        Fu      v1.0.0         创建
 */
@RestController
public class FileUploadController {

    /*@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
      @Retention(RetentionPolicy.RUNTIME)
      @Documented
      public @interface Value {
              String value();
      value声明周期是运行的时候加载
     */
    @Value("${youku1327.file.root.path}")
    private String fileRootPath;


    @RequestMapping(value = "/file/upload",method = RequestMethod.POST)
    public String fileUpload(@RequestParam("files") MultipartFile[] files){

        String filePath="";
        for (MultipartFile file:files){
            //获取文件名
            String originalFilename=file.getOriginalFilename();
            //创建文件存储路径
            filePath=new StringBuilder(fileRootPath)
                    .append(System.currentTimeMillis())
                    .append(originalFilename)
                    .toString();
            try {
                file.transferTo(new File(filePath));
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return filePath;
    }


    /*校验文件*/
    private String check(MultipartFile file){

        //校验文件类型
        String contentType=file.getContentType();
        List<String> allowType=new ArrayList<>();
        if (!allowType.contains(contentType)){
            return "类型校验不通过";
        }

        //校验文件内容
        try {
            BufferedImage image= ImageIO.read(file.getInputStream());
            if (image==null||image.getWidth()==0||image.getHeight()==0){
                return "内容不能为空";
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        /*//3.上传文件至fdfs服务器
        try {
           StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null);
        } catch (IOException e) {
            logger.error("上传文件失败...{}" + file.getName(), e);
        }*/
        return "200,成功";
    }

}

代码copy可以直接使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值