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; ...
随机推荐
- 键盘按键keyCode大全,js页面快捷键
字母和数字键的键码值(keyCode) 按键 键码 按键 键码 按键 键码 按键 键码 A 65 J 74 S 83 1 49 B 66 K 75 T 84 2 50 C 67 L 76 U 85 3 ...
- SSO单点登录三种情况的实现方式详解(转)
https://blog.csdn.net/ainuser/article/details/65631713
- 学习 JavaScript 树
学习 JavaScript 树 树(Tree) 在计算机科学中,数据结构是一门研究非数值计算的程序设计问题中计算机的操作对象(数据元素)以及它们之间的操作和运算等的学科. 它包含三方面的内容: 数据的 ...
- 【Linux】unix/Linux常用命令英文全称
英文全称解释更容易理解 知其然,更要知其所以然 man: Manual 意思是手册,可以用这个命令查询其他命令的用法. pwd:Print working directory 显示当前工作路径. su ...
- PopupWindow简单使用
如图是效果图 当点击 “点我”的按钮是 会弹出 如图的 弹窗 补充为PopupWindow设置一个显示动画和消失的动画 先在anim的文件下分别设置显示和消失的动画 <?xml versio ...
- DrawerLayout侧滑
DrawerLayout是Support Library包中实现了侧滑菜单效果的控件,可以说DrawerLayout是因为第三方控件如SlidingMenu等出现之后,google借鉴而出现的产物.D ...
- LeetCode刷题系列——Add Two Numbers
题目链接 这个题目很简单,归并而已,好久没练编程,居然忘了在使用自定义类型前,要进行初始化(new操作). class ListNode{ int val; ListNode next; ListNo ...
- wxPython控件学习之wx.grid.Grid 表格控件
wxPython控件学习之wx.grid.Grid (包括对GridCellEditor和GridCelRender的扩展,以支持更多的grid cell 样式, 以GridCellColorEdit ...
- Latex 中cite的使用
(一).设置上标显示的命令 可以在引言区增加类似如下的重定义命令: \newcommand{\upcitep}[1]{\textsuperscript{\textsuperscript{\cite ...
- jQuery Event.stopImmediatePropagation() 函数详解
stopImmediatePropagation()函数用于阻止剩余的事件处理函数的执行,并防止当前事件在DOM树上冒泡. 根据DOM事件流机制,在元素上触发的大多数事件都会冒泡传递到该元素的所有祖辈 ...