springboot优雅的返回图片--数据采集

springboot优雅的返回图片--数据采集

方式一:返回字节流

package com.ciwei.dcs.controller;

import org.springframework.http.MediaType;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;

/**
 * @author fuhang
 * @description: TODO
 * @date 2019/11/2216:15
 */
@RestController
@RequestMapping(value="/api/v1")
public class ImageTest {

    @GetMapping(value = "/image",produces = MediaType.IMAGE_JPEG_VALUE)
    @ResponseBody
    public byte[] test() throws Exception {
        File file = ResourceUtils.getFile((ResourceUtils.CLASSPATH_URL_PREFIX + "springboot.jpg"));
        FileInputStream inputStream = new FileInputStream(file);
        byte[] bytes = new byte[inputStream.available()];
        inputStream.read(bytes, 0, inputStream.available());
        return bytes;
    }

}

方式二:利用spring自带的图片格式

package com.ciwei.dcs.controller;

import com.alibaba.fastjson.JSONObject;
import com.ciwei.app.common.*;
import com.ciwei.app.utils.GeoUtil;
import com.ciwei.activemq.producer.topic.TopicSender;
import com.ciwei.dcs.base.entity.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * app上报日志
 * @author FuHang
 * @date 2019/11/1
 */
@Controller
@RequestMapping("/coll")
public class CollectLogController {
    private final Logger logger = LoggerFactory.getLogger(CollectLogController.class);

    @RequestMapping(value = "/pc",method = RequestMethod.GET,produces = MediaType.IMAGE_JPEG_VALUE)
    @ResponseBody
    public BufferedImage getImg(@RequestParam String json, HttpServletRequest req) throws IOException {
        logger.info("--------->获取到的json是:{}",json);
        File file = ResourceUtils.getFile((ResourceUtils.CLASSPATH_URL_PREFIX + "springboot.jpg"));
        return ImageIO.read(new FileInputStream(file));
    }
}
  1. 该方式需要配置转换器,不然报406错误
package com.ciwei.dcs.base.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.BufferedImageHttpMessageConverter;

/**
 * @author fuhang
 * @description: TODO
 * @date 2019/11/2216:31
 */
@Configuration
public class ImageConvert {
    @Bean
    public BufferedImageHttpMessageConverter bufferedImageHttpMessageConverter(){
        return new BufferedImageHttpMessageConverter();
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值