@GetMapping(value = "/template-preview",produces = MediaType.IMAGE_JPEG_VALUE)
public BufferedImage getImage(@RequestParam(name = "id") String id) throws Exception{
return printService.getImage(id);
@Override
public BufferedImage getImage(String id ) {
Example example = Example.builder(PrintTemplateInfo.class).build();
Example.Criteria criteria =example.createCriteria();
criteria.andEqualTo("id",id);
criteria.andEqualTo("delFlag",DelFlagEnum.NO_DELETE.getType());
PrintTemplateInfo printTemplateInfo = printTemplateInfoMapper.selectOneByExample(example);
if(printTemplateInfo!=null&&!StringUtils.isEmpty(printTemplateInfo.getPicture())) {
try {
File sourceFile = new File(printTemplateInfo.getPicture());
FileInputStream fileinputStream = null;
fileinputStream = new FileInputStream(sourceFile);
log.info("打印小图片{}", fileinputStream);
return ImageIO.read(fileinputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
图片转接器
package com.archs.oa.bizcommon.configuration;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.BufferedImageHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;
@Configuration
public class WebMvcConverterConfig implements WebMvcConfigurer {
/**
* 增加图片转换器
* @param converters
*/
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new BufferedImageHttpMessageConverter());
}
}