没想到啊没想到,还能在这么改,没想到还会再来记录个三,前两次写好了返给前端图片base64流了,自测也能出来图片,但是呢,前端人员说他没法给显示出来,没法给显示出来,没法给显示出来。。。。。。。。。。,问他能显示出来啥样的格式,说是给他文件流的话,就能显示,好,给他文件流,咱继续改接口,并且还要这一个接口功能变成:是图片,进行登录权限校验,是压缩包zip,进行下载,是音频或者视频,不校验,直接以流的形式返给前端展示,改代码后,结果如下:
先说下,我这是在linux环境部署的项目运行。
package com.xxx.micro.safemanageexternal.controller;
import com.xxx.micro.common.util.ResultUtil;
import com.xxx.micro.common.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.rmi.server.UID;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Encoder;
/**
* @ClassName: PictureController
* @Description:
* @author:
* @create: 2021/12/3 14:28
*/
@RestController
@Api(tags = "接口改造-图片拦截")
@Slf4j
@RequestMapping("/xxx")
public class PictureController extends BaseController {
@GetMapping("/xxx/xxx/{url}")
@ApiOperation(value = "查询图片,根据uid是否有效进行拦截", notes = "查询图片,根据uid是否有效进行拦截")
@ApiImplicitParams({
@ApiImplicitParam(name = "UID", value = "用户UID", required = true, paramType = "query"),
@ApiImplicitParam(name = "type", value = "文件格式类型", required = true, paramType = "query"),
@ApiImplicitParam(name = "url", value = "文件名称", required = true, paramType = "query")
})
public String weblogic(@PathVariable String url, @RequestParam String type, String UID, HttpServletResponse response) {
if (StringUtils.isBlank(url) || StringUtils.isBlank(type)) {
return this.responseJson(ResultUtil.error("参数缺失不完整"), null, null);
}
String path = "/xxx/xxx/xxx/" + url + "." + type;
// String path = "G:\\" + url + "." + type;
log.info("******文件路径是:" + path + "*********************");
try {
if (StringUtils.equalsIgnoreCase("zip", type)) {
//zip文件--下载
// 读到流中
InputStream inStream = new FileInputStream(path);// 文件的存放路径
// 设置输出的格式
response.reset();
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment; filename=\"" + url + "." + type + "\"");
// 循环取出流中的数据
byte[] b = new byte[100];
int len;
while ((len = inStream.read(b)) > 0) {
response.getOutputStream().write(b, 0, len);
}
inStream.close();
} else {
log.info("******查询的文件类型是:" + type + "*********************");
//判断文件类型(jpg,jpeg,bmp,png,gif,tiff),如果是图片类型--进行拦截,校验UID,非图片不校验
if (StringUtils.equalsIgnoreCase("jpg", type) || StringUtils.equalsIgnoreCase("jpeg", type) || StringUtils
.equalsIgnoreCase("bmp", type) || StringUtils.equalsIgnoreCase("png", type) || StringUtils
.equalsIgnoreCase("gif", type) || StringUtils.equalsIgnoreCase("tiff", type)) {
//参数-UID判空
if (StringUtils.isBlank(UID)) {
return this.responseJson(ResultUtil.error("参数UID缺失"), null, null);
}
//查询当前登录人
Map<String, Object> loginUser = this.getLoginUser();
if (loginUser == null) {
return this.responseJson(ResultUtil.error("查询当前登录人失败"), null, null);
}
}
File files = new File(path);
//文件判空
if (null == files || 0 == files.length() || !files.exists()) {
System.out.println("文件为空!");
log.error("文件为空!");
return "文件为空";
}
//将文件转换成流
FileInputStream inputFile = new FileInputStream(files);
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
//创建存放文件内容的数组
byte[] buff = new byte[1024];
//所读取的内容使用n来接收
int n;
//当没有读取完时,继续读取,循环
while ((n = inputFile.read(buff)) != -1) {
//将字节数组的数据全部写入到输出流中
outputStream.write(buff, 0, n);
}
//强制将缓存区的数据进行输出
outputStream.flush();
//关流
outputStream.close();
inputFile.close();
}
return null;
} catch (Exception e) {
log.error("异常信息", e);
return null;
}
}
}