response java 返回_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();

}

}

}


版权声明:本文为weixin_39574140原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。