• 使用springboot上传文件至nginx代理服务器


    nginx配置图片服务器

        server {
        listen       8001;
        server_name  image.xxx.com;
    
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
        location / {
            # 放置物理路径
            root C:\zysuyuan\image;
            }
        }

    一、编写application.yml

    server:
      port: 8082
    spring:
      application:
        name: upload-service
      servlet:
        multipart:
          max-file-size: 5MB
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:10086/eureka
      instance:
        lease-renewal-interval-in-seconds: 5  # 心跳时间5s
        lease-expiration-duration-in-seconds: 15  #  过期时间15s

    二、编写Controller

    @Controller
    @RequestMapping("upload")
    public class UploadController {
    
        @Autowired
        private UploadService uploadService;
    
        @PostMapping("image")
        public ResponseEntity<String> uploadImage(@RequestParam("file")MultipartFile file){
            String url = this.uploadService.uploadImage(file);
            if (StringUtils.isBlank(url)){
                return ResponseEntity.badRequest().build();
            }
            return ResponseEntity.status(HttpStatus.CREATED).body(url);
        }
    }

    三、编写Service

    @Service
    public class UploadService {
    
        // 制作文件的白名单,使用Arrays.asList列出一个常量的字符串
        private static final List<String> CONTENT_TYPES = Arrays.asList("image/gif","image/jpeg","image/png");
        // 定义输出错误日志
        private static final Logger LOGGER = LoggerFactory.getLogger(UploadService.class);
    
        public String uploadImage(MultipartFile file) {
    
            // 获取文件类型
            String originalFilename = file.getOriginalFilename();
    
            try {
                // 校验文件类型
                // 列出所有文件合法类型
                String contentType = file.getContentType();
                if (!CONTENT_TYPES.contains(contentType)){
                    LOGGER.info("文件类型不合法:{}",originalFilename);
                    return null;
                }
    
                // 校验文件的内容,ImageIO读取文件内容
                BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
                if (bufferedImage == null) {
                    LOGGER.info("文件内容不合法:{}",originalFilename);
                    return null;
                }
    
                // 保存到服务器
                file.transferTo(new File("C:\zysuyuan\image\" + originalFilename));
    
                // 返回url,进行回显
                return "http://image.xxx.com/" + originalFilename;
    
            } catch (IOException e) {
                LOGGER.info("服务器内部错误:" + originalFilename);
                e.printStackTrace();
            }
            return null;
        }
    }

    测试:使用Advanced-REST-client进行文件上传测试

    链接:https://pan.baidu.com/s/10Ax3eSY5zCwRRMHBDI-1sw
    提取码:cjhq

    最后,可以通过访问nginx配置的web服务器,访问对于图片的url来测试文件是否上传成功。

  • 相关阅读:
    Stream 和 byte[] 之间的转换
    C# Process类_进程_应用程序域与上下文之间的关系
    C# Process类_进程管理器Demo
    C# attribute_特性
    SqlDataAdapter类
    SqlDataReader类
    SqlCommand类
    SqlConnection类
    DataTable类
    C# 语法技巧_三目运算_switch_case
  • 原文地址:https://www.cnblogs.com/flypig666/p/11747548.html
Copyright © 2020-2023  润新知