zoukankan      html  css  js  c++  java
  • 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");
    }
    
  • 相关阅读:
    从获取QQ验证码谈如何改进用户体验,提高程序的响应效果
    如何利用C#批量注册QQ邮箱
    利用DotRAS组件,实现ADSL的自动拨号断网自动化操作
    探讨如何利用C#登录QQ邮箱进行群邮件的发送
    利用C#开发基于snmpsharpnet基础的SNMP开发应用
    QQ窗口抓取及如何进行自动化操作
    对比三种GoogleMap图标操作处理,谈如何构造快速响应的GoogleMap图标叠加操作
    Winform下的地图开发控件(GMap.NET)使用心得之三批量解析地址经纬度坐标
    基于Lumisoft.NET实现的邮件发送功能
    谈谈数据加密的处理提供各种算法处理
  • 原文地址:https://www.cnblogs.com/tenny-peng/p/13092046.html
Copyright © 2011-2022 走看看