package com.fzzy.api.service; import com.fzzy.api.entity.Api1109; import com.fzzy.api.view.repository.Api1109Rep; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileInputStream; import java.io.IOException; /** * @author vince.xu * @Title: ImgService * @ProjectName igds-api * @Description: TODO * @date 2022-9-611:25 */ @Slf4j @Controller @RequestMapping public class ImgService { @Autowired private Api1109Rep api1109Rep; /** * IO流读取图片 by:long * @return */ @RequestMapping(value = "/img/{imgId}", method = RequestMethod.GET) public String IoReadImage(@PathVariable String imgId, HttpServletRequest request, HttpServletResponse response) throws IOException { ServletOutputStream out = null; FileInputStream ips = null; try { //获取图片存放路径 Api1109 api1109 = api1109Rep.findById(imgId).get(); ips = new FileInputStream(new File(api1109.getWjdz())); response.setContentType("multipart/form-data"); out = response.getOutputStream(); //读取文件流 int len = 0; byte[] buffer = new byte[1024 * 10]; while ((len = ips.read(buffer)) != -1){ out.write(buffer,0,len); } out.flush(); } catch (Exception e){ log.error(e.getMessage(),e); } finally { if(out != null ) out.close(); if(ips != null ) ips.close(); } return null; } }