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. C# 安装WindowService服务和相关

    https://www.cnblogs.com/charlie-chen2016/p/8031774.html 这是一个备份数据库的服务,逻辑很简单,就是通过定时器实现在特定的时间执行SQL语句备份数 ...

  2. 二、MVC3+EF单表增删改查

    document 表为例 写入静态类 NorthwindDataProvider: Controller可直接调用:如 //获取document表全部数据 NorthwindDataProvider. ...

  3. log4net写入DB2备忘 via OLEDB & ODBC

    在项目中遇到需要记录操作日志的需求,由于是一个外挂系统,因此不用考虑到公司框架的限制,直接二层架构直连数据库,考虑使用log4net连接DB2.请宽恕我这个非软工科班出身的IT小白,以前一直在知道有个 ...

  4. [转]最全Redis面试题整理

    此为转载文章,仅做记录使用,方便日后查看,原文链接:http://www.bieryun.com/3405.html 1.什么是Redis? 答:Redis全称为:Remote Dictionary ...

  5. L1-002 打印沙漏 (20 分)

    L1-002 打印沙漏 (20 分) 方法:清晰思路,纸上写出实例,注意循环使用 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个“*”,要求按下列格式打印 ***** *** * ** ...

  6. 静态化HttpClient

    实现方法: public class HttpClientHelper { private static HttpClient _client; public static HttpClient Ge ...

  7. 集合之Iterator迭代器

      Iterator迭代器概述: java中提供了很多个集合,它们在存储元素时,采用的存储方式不同.我们要取出这些集合中的元素,可通过一种通用的获取方式来完成. Collection集合元素的通用获取 ...

  8. 从零开始的全栈工程师——jQuery

    jQueryjq是js一个高效且精简的库( 用的多写得少 ) ,是chrome出版的.jq内部有一个$的方法,他是jq的起始符或标识符,这个方法是用于获取元素. 下载库或者框架的方法官网 produc ...

  9. css 简单梯形

    通过css2D变形我们可以轻松得到平行四边形,那么通过此技巧可以得到梯形吗? no! 不过我们可以通过3D旋转得到类似这样的效果: transform:perspective(0.5em)  rota ...

  10. 003Angular2中使用ng-bootstrap

    1.检查@angular/cli版本 命令行ng -v ,版本号必须大于1.0.0-beta.24 2.新建工程 工程所在目录,命令行ng new my-app --style=scss 带style ...