java 后台返回文件流到浏览器
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 后台返回文件流到浏览器的更多相关文章
- webapi返回文件流
		逻辑说明 webapi返回类型为IHttpActionResult接口,内部方法返回HttpResponseMessage. public interface IHttpActionResult { ... 
- Ajax异步请求返回文件流(eg:导出文件时,直接将导出数据用文件流的形式返回客户端供客户下载)
		在异步请求中要返回文件流,不能使用JQuery,因为$.ajax,$.post 不支持返回二进制文件流的类型,可以看到下图,dataType只支持xml,json,script,html这几种格式,没 ... 
- java 后台通过IO流把文件传到前端并下载
		我的业务需求是两个不同的web程序放在不同的服务器上,web程序A要访问到web程序B上传上来的文件,所以用到了这一个IO读取文件的接口 JAVA代码(排版有点问题 已经尽力补救了(:3_ヽ)_) ... 
- 文件下载post请求,返回文件流转换zip,
		最近一个需求是批量下载文件,最后打包成zip包,post请求, 因为是文件流下载,所以在取后台数据的时候,要多传递一个[responseType: ‘blob’]这个参数 download() { t ... 
- vue项目 下载表格 java后台返回的是信息流表格如何下载解决乱码
		主要是在请求参数后面加上{responseType: 'blob'}或者arrayBuffer this.$http.get(this.api.export, { params: this.info, ... 
- java后台生成文件给前端下载(response输出流)
		1.设置 ContentType response.setContentType("application/x-download"); 2.设置文件名,并指定编码格式 fileNa ... 
- Java  读取excel  文件流
		public static void initType(String path) { try { //1 获取excel文件流 excel xls 文件 暂不支持xlsx if (path.conta ... 
- java中打开文件流后要关闭后重新打开
		记录错误,打开文件流一定要关闭并重新打开文件流,不然取的文件内容永远是第一次取的文件内容: /** * 读取配置文件 */ private Properties readProperties() { ... 
- java后台调用HttpURLConnection类模拟浏览器请求(一般用于接口调用)
		项目开发中难免遇到外部接口的调用,小生今天初次接触该类,跟着API方法走了一遍,如有不对的地方,还请哆哆指正,拜谢! 1 package com.cplatform.movie.back.test; ... 
随机推荐
- [转]ASP.NET Web API系列教程(目录)
			本文转自:http://www.cnblogs.com/r01cn/archive/2012/11/11/2765432.html 注:微软随ASP.NET MVC 4一起还发布了一个框架,叫做ASP ... 
- OC与JS交互之WebViewJavascriptBridge
			上一篇文章介绍了通过UIWebView实现了OC与JS交互的可能性及实现的原理,并且简单的实现了一个小的示例DEMO,当然也有一部分遗留问题,使用原生实现过程比较繁琐,代码难以维护.这篇文章主要介绍下 ... 
- C/C++中的auto关键词
			C语言 auto被解释为一个自动存储变量的关键字,也就是申明一块临时的变量内存. 例如: auto double a=3.7; 表示a为一个自动存储的临时变量. C++语言 C++ 98标准/C++0 ... 
- numpy中一些常用函数的用法总结
			先简单记录一下,后续补充详细的例子 1. strip()函数 s.strip(rm):s为字符串,rm为要删除的字符序列 只能删除开头或是结尾的字符或者字符串.不能删除中间的字符或是字符串 当rm ... 
- 转 【<meta name="description" content=">】作用讲解
			今天在看别人写的网站代码,发现类似<meta name="Keywords" content="" >.<meta name="De ... 
- .NET开源工作流RoadFlow-表单设计-数据表格
			数据表格即在表单中显示一个table,该table数据可以来自任意自定义的来源: 数据类型:指定表格的数据源类型 1.datatable,即.net中的System.Data.DataTable 2. ... 
- Highcharts - Pie Chart
			1. 饼状图(Pie Chart)示例: <div id="container" style="height: 400px"></div> ... 
- Polly一种.NET弹性和瞬态故障处理库(重试策略、断路器、超时、隔板隔离、缓存、回退、策略包装)
			下载地址:https://github.com/App-vNext/Polly 该库实现了七种恢复策略. 重试策略(Retry) 重试策略针对的前置条件是短暂的故障延迟且在短暂的延迟之后能够自我纠正. ... 
- Write a makefile to compile *.c and link to executable target
			https://wenku.baidu.com/view/b1ec946027d3240c8447ef9a.html GNU+make中文手册V3.8 <=========From Docs== ... 
- Resharper安装及激活--转载
			原文地址:ReSharper2018破解详细方法 1.先安装好Resharper: 2.下载完补丁后解压,以管理员身份运行Patch.cmd,如下图所示,即破解成功: 3.打开VS,打开ReSha ... 
