package com.springbootblog.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*; /**
*@title : JavaClass
*@author:zyh
*@createDate:2018/9/13 21:46
*
**/
@RequestMapping(value = "/queryImg")
@Controller
public class ReadImgController {
@ApiOperation(value = "img", produces = "application/json")
@ApiImplicitParams({
})
@RequestMapping(value = "/img",method = RequestMethod.POST)
@ResponseBody
public void getImage(String path, HttpServletRequest request, HttpServletResponse response) {
try {
String url="D:\\temp-appImg\\20180912\\7cd2e1a3-a087-4e25-aac8-2bdf8e274c6f.png";
File file = new File(url);
String l=request.getRealPath("/")+"/"+url;
String filename = file.getName();
InputStream fis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
// 设置response的Header
response.addHeader("Content-Length", "" + file.length());
response.setContentType("image/jpg"); OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
//return response;
} @ApiOperation(value = "img", produces = "application/json")
@ApiImplicitParams({
})
@RequestMapping(value = "/imgRes",method = RequestMethod.POST)
@ResponseBody
public HttpServletResponse getImageRes(String path, HttpServletRequest request, HttpServletResponse response) {
try {
String url="D:\\temp-appImg\\20180912\\7cd2e1a3-a087-4e25-aac8-2bdf8e274c6f.png";
File file = new File(url);
String l=request.getRealPath("/")+"/"+url;
String filename = file.getName();
InputStream fis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
// 设置response的Header
response.addHeader("Content-Length", "" + file.length());
response.setContentType("image/png"); OutputStream toClient = new BufferedOutputStream(response.getOutputStream(),888888);
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return response;
}
@ApiOperation(value = "返回指定地址的文件流1")
@ApiImplicitParams({
@ApiImplicitParam(name = "url", value = "图片地址", required = true,
paramType = "query", defaultValue = "\\20180912\\7cd2e1a3-a087-4e25-aac8-2bdf8e274c6f.png"),
})
@RequestMapping(value = "/noLogin/readImageFile1", method = RequestMethod.POST)
@ResponseBody
public void getUrlFile(String url, HttpServletRequest request, HttpServletResponse response) {
String serverUrl = "D:\\temp-appImg\\";
String imgUrl = serverUrl + url;
File file = new File(imgUrl);
// 后缀名
String suffixName = url.substring(url.lastIndexOf("."));
String imgType = "image/" + suffixName;
//判断文件是否存在如果不存在就返回默认图标
if (!(file.exists() && file.canRead())) {
file = new File(request.getSession().getServletContext().getRealPath("/")
+ "resource/icons/auth/root.png");
}
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
int length = inputStream.read(data);
inputStream.close();
//setContentType("text/plain; charset=utf-8"); 文本
response.setContentType(imgType + ";charset=utf-8");
OutputStream stream = response.getOutputStream();
stream.write(data);
stream.flush();
stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

java 后台返回文件流到浏览器的更多相关文章

  1. webapi返回文件流

    逻辑说明 webapi返回类型为IHttpActionResult接口,内部方法返回HttpResponseMessage. public interface IHttpActionResult { ...

  2. Ajax异步请求返回文件流(eg:导出文件时,直接将导出数据用文件流的形式返回客户端供客户下载)

    在异步请求中要返回文件流,不能使用JQuery,因为$.ajax,$.post 不支持返回二进制文件流的类型,可以看到下图,dataType只支持xml,json,script,html这几种格式,没 ...

  3. java 后台通过IO流把文件传到前端并下载

    我的业务需求是两个不同的web程序放在不同的服务器上,web程序A要访问到web程序B上传上来的文件,所以用到了这一个IO读取文件的接口 JAVA代码(排版有点问题  已经尽力补救了(:3_ヽ)_) ...

  4. 文件下载post请求,返回文件流转换zip,

    最近一个需求是批量下载文件,最后打包成zip包,post请求, 因为是文件流下载,所以在取后台数据的时候,要多传递一个[responseType: ‘blob’]这个参数 download() { t ...

  5. vue项目 下载表格 java后台返回的是信息流表格如何下载解决乱码

    主要是在请求参数后面加上{responseType: 'blob'}或者arrayBuffer this.$http.get(this.api.export, { params: this.info, ...

  6. java后台生成文件给前端下载(response输出流)

    1.设置 ContentType response.setContentType("application/x-download"); 2.设置文件名,并指定编码格式 fileNa ...

  7. Java 读取excel 文件流

    public static void initType(String path) { try { //1 获取excel文件流 excel xls 文件 暂不支持xlsx if (path.conta ...

  8. java中打开文件流后要关闭后重新打开

    记录错误,打开文件流一定要关闭并重新打开文件流,不然取的文件内容永远是第一次取的文件内容: /** * 读取配置文件 */ private Properties readProperties() { ...

  9. java后台调用HttpURLConnection类模拟浏览器请求(一般用于接口调用)

    项目开发中难免遇到外部接口的调用,小生今天初次接触该类,跟着API方法走了一遍,如有不对的地方,还请哆哆指正,拜谢! 1 package com.cplatform.movie.back.test; ...

随机推荐

  1. 两个三汇API使用的坑

    最近呼叫中心走火入魔了,我的<一步一步开发呼叫中心>系列编写过程中,遇到各种的问题,今天晚上,来记录一下纠结了我N久的一个问题: 内线通过板卡外呼时,如果对方的呼叫中心需要发送按键响应(如 ...

  2. spring+springmvc+mybatis 开发JAVA单体应用

    myshop 概述 myshop项目是根据视频教程 Java 单体应用 做的一个练习项目,目前完成了登录功能.用户管理.类别管理后续有时间会继续做其它的功能.趁着双11花了99元一年买了台阿里云服务器 ...

  3. 线程的Interrupt方法与InterruptedException解析

    线程阻塞状态与等待状态(当一个线程处于被阻塞或等待状态时,它暂时不活动,不允许任何代码且消耗最少的资源) 当一个线程试图获得一个内部的对象锁(而不是java.util.concurrent库中的锁), ...

  4. 位运算(3)——Reverse Bits

    翻转32位无符号二进制整数 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (r ...

  5. Linux 套接字编程 - TCP连接基础

    第五章的内容,实现一个echo服务器和对应的客户端,主要收获: 0. TCP socket编程主要基本步骤 1. SIGCHLD信号含义(子进程退出时向父进程发送,提醒父进程对其状态信息进行一个获取) ...

  6. 靠谱的div引入任何外链内容

    靠谱的div引入任何外链内容 开发中经常要在div中引入一个页面,该页面可能是内部页面,可能是一个外部页面,也可能只是一个域名获取的请求. 对于内部页面的加载,建议使用jquery的load函数,如: ...

  7. php有经纬度计算距离

    /** *  @desc 根据两点间的经纬度计算距离 *  @param float $lat 纬度值 *  @param float $lng 经度值 */  function getDistanc ...

  8. .NET开源工作流RoadFlow-表单设计-HTML编辑器

    roadflow目前采用的html编辑器为ueditor编辑器(和表单设计器的编辑器相同). 绑定字段:与数据表的某个字段对应. 宽度:编辑器的宽度. 高度:编辑器的高度. 运行效果如下:

  9. 详细分析UserLock如何保证高校内部信息安全

    俄克拉荷马城市公立学校的IT团队负责该片区接近43000个学生的网络管理工作.长期以来,学生和教师员工共享Windows网络登录为他们带来了很多难题. 由于没有并发登录的限制,也不能对网络使用情况进行 ...

  10. DevExpress控件水印文字提示 z

    ButtonEdit.Properties.NullValuePrompt = "提示"; ButtonEdit.Properties.NullValuePromptShowFor ...