zoukankan      html  css  js  c++  java
  • 使用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来测试文件是否上传成功。

  • 相关阅读:
    【机器学习】sklearn之生成三类数据用于聚类,python
    【机器学习】sklearn之生成环形和半环型数据,python
    【机器学习】sklearn之创建数据集,python
    【python】使用plotly画三维立体高逼格图,数据可视化
    【python】使用jieba中的textrank提取文本/文章关键词,超简单!
    【python】使用jieba提取文本关键词,超简单!
    【机器学习】gensim.models.Word2Vec()参数的详细解释,python
    XP系统老电脑如何安装Linux系统
    PHP中派生是什么?
    MySQL中CURD是什么?
  • 原文地址:https://www.cnblogs.com/flypig666/p/11747548.html
Copyright © 2011-2022 走看看