OpenFeign文件上传下载

记录一下OpenFeign文件上传下载接口

上传

服务提供方接口

@PostMapping("upload")
public void upload(String fileOssName, @RequestBody byte[] bytes) {
	OSS ossClient = new OSSClientBuilder().build(END_POINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
	ossClient.putObject(BUCKET_NAME_PC, fileOssName, new ByteArrayInputStream(bytes));
	ossClient.shutdown();
}

服务接口直接接收byte数组,bytes参数要用@RequsetBody接收

这里最终上传到阿里云OSS,换成其他目的地流都类似。

byte数组可以转化为InputStream: new ByteArrayInputStream(bytes)

服务调用方的远程接口

@FeignClient("oss-service")
public interface OssFeignService {
	
	@PostMapping("oss/upload")
	public Result upload(@RequestParam String fileOssName, @RequestBody byte[] bytes);
	
}

服务调用方接口

ossFeignService.upload(fileOssName, bytes);

bytes来源为文件或内存,具体看自己业务

下载

服务提供方接口

@GetMapping("download")
public byte[] download(String fileOssName) throws Exception {
	OSS ossClient = new OSSClientBuilder().build(END_POINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
	OSSObject ossObject = ossClient.getObject(BUCKET_NAME_APP, fileOssName);
	InputStream in = ossObject.getObjectContent();
	
	ByteArrayOutputStream out = new ByteArrayOutputStream();
	IoUtils.copy(in, out);
	
	out.close();
	in.close();
	ossClient.shutdown();
	
	return out.toByteArray();
}

这里的InputStream来源于阿里云OSS,换成其他输入流都类似

IoUtils是OSS提供的工具类,copy方法其实就是我们平时写的二进制流的读写,源码如下

static public long copy(InputStream input, OutputStream output) throws IOException {
	byte[] buffer = new byte[1024];
	int bytesRead;
	int totalBytes = 0;
	while ((bytesRead = input.read(buffer)) != -1) {
		output.write(buffer, 0, bytesRead);

		totalBytes += bytesRead;
	}

	return totalBytes;
}

服务调用方的远程接口

@FeignClient("oss-service")
public interface OssFeignService {
	
	@GetMapping("oss/download")
	public byte[] download(@RequestParam String fileOssName);
	
}

服务调用方接口

@GetMapping("download")
public void download(String fileOssName, String fileName, HttpServletRequest request, HttpServletResponse response) throws Exception {
	ExcelUtil.setResponseHeader(request, response, fileName);
	
	byte[] bytes = thirdPartyFeignService.downloadFromPc(fileOssName);
	OutputStream out = response.getOutputStream();
	
	out.write(bytes);
	out.flush();
	out.close();
}

其中setResponseHeader方法设置下载头信息

public static void setResponseHeader(HttpServletRequest request, HttpServletResponse response, String fileName) throws UnsupportedEncodingException {
	String agent = request.getHeader("USER-AGENT");
	if (null != agent && -1 != agent.indexOf("MSIE") || null != agent && -1 != agent.indexOf("Trident") || null != agent && -1 != agent.indexOf("Edge")) {// ie
		fileName = java.net.URLEncoder.encode(fileName, "UTF8");
	} else if (null != agent && -1 != agent.indexOf("Mozilla")) {// 火狐,chrome等
		fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
	}

	response.setContentType("application/octet-stream; charset=utf-8");
	response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
	response.addHeader("Pargam", "no-cache");
	response.addHeader("Cache-Control", "no-cache");
}
posted @ 2020-06-11 11:13  淘气小饼干  阅读(1881)  评论(0编辑  收藏  举报