blob: ea307b9698786c3a0c8beeb84f1784823d534973 [file] [log] [blame]
<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="shortcut icon" href="../img/favicon.ico" />
<title>文件下载开发指导 - ServiceComb Java Chassis 开发指南</title>
<link rel="stylesheet" href="../css/theme.css" />
<link rel="stylesheet" href="../css/theme_extra.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/github.min.css" />
<script>
// Current page data
var mkdocs_page_name = "\u6587\u4ef6\u4e0b\u8f7d\u5f00\u53d1\u6307\u5bfc";
var mkdocs_page_input_path = "general-development/file-download.md";
var mkdocs_page_url = null;
</script>
<script src="../js/jquery-3.6.0.min.js" defer></script>
<!--[if lt IE 9]>
<script src="../js/html5shiv.min.js"></script>
<![endif]-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="../index.html" class="icon icon-home"> ServiceComb Java Chassis 开发指南
</a>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<ul>
<li class="toctree-l1"><a class="reference internal" href="../toc.html">目录</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../index.html">概述</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../start/catalog.html">快速入门</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../start/design.html">设计选型参考</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../build-provider/definition/service-definition.html">微服务定义</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../build-provider/catalog.html">开发服务提供者</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../build-consumer/catalog.html">开发服务消费者</a>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="catalog.html">通用功能开发</a>
</li>
</ul>
<p class="caption"><span class="caption-text">多样化的通信协议功能参考</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../transports/introduction.html">多协议介绍</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../transports/rest-over-servlet.html">REST over Servlet</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../transports/rest-over-vertx.html">REST over Vertx</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../transports/http2.html">REST over HTTP2</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../transports/highway-rpc.html">Highway</a>
</li>
</ul>
<p class="caption"><span class="caption-text">多样化的服务注册与发现功能参考</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../registry/introduction.html">注册发现说明</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../registry/service-center.html">使用服务中心</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../registry/local-registry.html">本地注册发现</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../registry/distributed.html">去中心化注册发现</a>
</li>
</ul>
<p class="caption"><span class="caption-text">管理服务配置</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../config/general-config.html">通用配置说明</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../config/read-config.html">在程序中读取配置信息</a>
</li>
</ul>
<p class="caption"><span class="caption-text">服务治理功能参考</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../references-handlers/intruduction.html">处理链介绍</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../references-handlers/loadbalance.html">负载均衡</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../references-handlers/ratelimit.html">限流</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../references-handlers/router.html">灰度发布</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../references-handlers/fault-injection.html">故障注入</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../references-handlers/governance.html">流量特征治理</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../references-handlers/fail-retry.html">快速失败和重试</a>
</li>
</ul>
<p class="caption"><span class="caption-text">网关功能参考</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../edge/open-service.html">介绍</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../edge/by-servicecomb-sdk.html">使用 Edge Service 做网关</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../edge/zuul.html">使用 `zuul` 和 `spring cloud gateway` 做网关</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../edge/nginx.html">nginx 网关简单介绍</a>
</li>
</ul>
<p class="caption"><span class="caption-text">安全特性参考</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../references-handlers/publickey.html">公钥认证</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../security/tls.html">使用TLS通信</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../security/shi-yong-rsa-ren-zheng.html">使用RSA认证</a>
</li>
</ul>
<p class="caption"><span class="caption-text">服务打包和运行</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../packaging/standalone.html">以standalone模式打包</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../packaging/web-container.html">以WEB容器模式打包</a>
</li>
</ul>
<p class="caption"><span class="caption-text">专题文章</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../using-java-chassis-in-spring-boot/using-java-chassis-in-spring-boot.html">在Spring Boot中使用java chassis</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../featured-topics/features.html">新功能介绍系列文章</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../featured-topics/compatibility.html">兼容问题和兼容性策略</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../featured-topics/upgrading.html">升级指导系列文章</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../featured-topics/performance.html">性能问题分析和调优</a>
</li>
</ul>
<p class="caption"><span class="caption-text">常用配置项参考</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../config-reference/rest-transport-client.html">REST Transport Client 配置项</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../config-reference/config-center-client.html">Config Center Client 配置项</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../config-reference/service-center-client.html">Service Center Client 配置项</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../config-reference/kie-client.html">ServiceComb Kie Client 配置项</a>
</li>
</ul>
<p class="caption"><span class="caption-text">常见问题</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../question-and-answer/faq.html">FAQ</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../question-and-answer/question_answer.html">Q & A</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../question-and-answer/interface-compatibility.html">微服务接口兼容常见问题</a>
</li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="Mobile navigation menu">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">ServiceComb Java Chassis 开发指南</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content"><div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html" class="icon icon-home" alt="Docs"></a> &raquo;</li>
<li>文件下载开发指导</li>
<li class="wy-breadcrumbs-aside">
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div class="section" itemprop="articleBody">
<h1 id="_1">文件下载开发指导</h1>
<h2 id="_2">服务提供者开发</h2>
<p>文件下载支持采用 Spring MVC 和 Jax RS 开发。 因为文件下载都是 GET 方法, 因此两者的使用差异很小, 这里的例子只提供
Spring MVC 。 </p>
<ul>
<li>File</li>
</ul>
<p>最简单的例子,接口的返回参数声明为 File 类型的参数, 即可定义一个下载接口。 </p>
<pre><code>@GetMapping(path = &quot;/file&quot;)
public File file(String name)
</code></pre>
<ul>
<li>Part</li>
</ul>
<p>如果需要根据请求参数动态创建临时文件,下载完成后,将临时文件删除,可以采用 Part 类型的参数。</p>
<pre><code>@GetMapping(path = &quot;/file&quot;)
public Part file(String content) throws IOException {
File file = createTempFile(content);
return new FilePart(null, file)
.setDeleteAfterFinished(true)
.setSubmittedFileName(&quot;test.txt&quot;);
}
</code></pre>
<ul>
<li>Resource</li>
</ul>
<p>可以将接口声明为 org.springframework.core.io.Resource。 由于resource不一定表示文件下载,所以需要通过
@ApiResponse 标识这是一个文件下载场景</p>
<p>以ByteArrayResource为例说明:</p>
<pre><code>@GetMapping(path = &quot;/resource&quot;)
@ApiResponses({
@ApiResponse(code = 200, response = File.class, message = &quot;&quot;)
})
public Resource resource() {
return new ByteArrayResource(bytes) {
@Override
public String getFilename() {
return &quot;resource.txt&quot;;
}
};
}
</code></pre>
<p>上例中,因为ByteArrayResource没有文件名的概念,所以需要实现Resource的getFilename方法,也可以通过ResponseEntity
进行包装:</p>
<pre><code>@GetMapping(path = &quot;/resource&quot;)
@ApiResponses({
@ApiResponse(code = 200, response = File.class, message = &quot;&quot;)
})
public ResponseEntity&lt;Resource&gt; resource() {
return ResponseEntity
.ok()
.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE)
.header(HttpHeaders.CONTENT_DISPOSITION, &quot;attachment;filename=resource.txt&quot;)
.body(resource);
}
</code></pre>
<ul>
<li>InputStream</li>
</ul>
<p>一样的, 使用 InputStream 需要 @ApiResponse 标识这是一个文件下载场景</p>
<pre><code>@GetMapping(path = &quot;/inputStream&quot;)
@ApiResponses({
@ApiResponse(code = 200, response = File.class, message = &quot;&quot;),
})
public ResponseEntity&lt;InputStream&gt; download() throws IOException {
return ResponseEntity
.ok()
.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE)
.header(HttpHeaders.CONTENT_DISPOSITION, &quot;attachment;filename=resource.txt&quot;)
.body(stream);
}
</code></pre>
<p>在下载完成后,ServiceComb会自动关闭stream,开发人员不必再关注。</p>
<ul>
<li>文件类型判定</li>
</ul>
<p>只要没有通过ResponseEntity直接设置HttpHeaders.CONTENT_TYPE,ServiceComb都会尝试通过File、Part、Resource中的文件名后缀进行自动判定。</p>
<p>ServiceComb使用java的mime type机制进行文件类型判定,如果业务场景中的文件后缀无法被识别,ServiceComb会默认处理为application/octet-stream</p>
<p>如果这不满足要求,假设文件后缀为xyz,期望文件类型为application/file-xyz,以下方式任选一种均可解决:</p>
<ol>
<li>通过Java的mime type机制扩展</li>
</ol>
<p>在META-INF目录下,创建mime.types文件,其内容为:</p>
<pre><code>application/file-xyz xyz
</code></pre>
<pre><code>2. 在业务代码中通过Part指定
</code></pre>
<pre><code>@GetMapping(path = &quot;/tempFilePart&quot;)
public Part tempFilePart(String content) throws IOException {
File file = createTempFile(content);
return new FilePart(null, file)
.setDeleteAfterFinished(true)
.contentType(&quot;application/file-xyz&quot;)
.setSubmittedFileName(&quot;tempFilePart.txt&quot;);
}
</code></pre>
<pre><code>3. 在业务代码中通过ResponseEntity指定
</code></pre>
<pre><code>@GetMapping(path = &quot;/tempFileEntity&quot;)
public ResponseEntity&lt;Part&gt; tempFileEntity(String content) throws IOException {
File file = createTempFile(content);
return ResponseEntity
.ok()
.header(HttpHeaders.CONTENT_TYPE, &quot;application/file-xyz&quot;)
.header(HttpHeaders.CONTENT_DISPOSITION, &quot;attachment;filename=tempFileEntity.txt&quot;)
.body(new FilePart(null, file)
.setDeleteAfterFinished(true));
}
</code></pre>
<ul>
<li>指定文件名</li>
</ul>
<p>只要没有通过ResponseEntity直接设置HttpHeaders.CONTENT_DISPOSITION,ServiceComb都会尝试通过File、Part、Resource中的文件名生成HttpHeaders.CONTENT_DISPOSITION,假设文件名为file.txt,则生成的数据如下:</p>
<pre><code>Content-Disposition: attachment;filename=file.txt;filename*=utf-8’’file.txt
</code></pre>
<p>不仅仅生成filename,还生成了filename*,这是因为如果文件名中出现了中文、空格,并且filename正确地做了encode,ie、chrome都没有问题,但是firefox直接将encode后的串当作文件名直接使用了。firefox按照<a href="https://tools.ietf.org/html/rtf6266">https://tools.ietf.org/html/rtf6266</a>,只对filename*进行解码。</p>
<p>如果业务代码中直接设置Content-Disposition,需要自行处理多浏览器支持的问题。</p>
<p>文件下载的更多用法可以通过下载 java-chassis 源码, 查看 DownloadSchema 里面的例子。</p>
<h2 id="_3">服务消费者开发</h2>
<p>消费者统一使用 org.apache.servicecomb.foundation.vertx.http.ReadStreamPart 处理文件下载。
可以使用透明 RPC 方式, </p>
<pre><code>public interface ……{
ReadStreamPart download1(……);
ReadStreamPart download2(……);
}
</code></pre>
<p>或者 RestTemplate</p>
<pre><code>ReadStreamPart part = restTemplate.getForObject(url, ReadStreamPart.class);
</code></pre>
<p>ReadStreamPart提供了一系列方法,将数据流保存为本地数据:</p>
<pre><code>org.apache.servicecomb.foundation.vertx.http.ReadStreamPart.saveAsBytes()
org.apache.servicecomb.foundation.vertx.http.ReadStreamPart.saveAsString()
org.apache.servicecomb.foundation.vertx.http.ReadStreamPart.saveToFile(String)
org.apache.servicecomb.foundation.vertx.http.ReadStreamPart.saveToFile(File, OpenOptions)
</code></pre>
<p><strong><em>注意:</em></strong></p>
<ul>
<li>在得到ReadStreamPart实例时,并没有完成文件内容的下载,调用save系列方法才开始真正从网络上读取文件数据。</li>
<li>如果使用saveAsBytes、saveAsString,数据是直接保存在内存中的,如果下载的文件很大,会导致内存溢出。</li>
<li>save系列方法,返回的都是CompletableFuture对象, 如果要阻塞等待下载完成,通过future.get()即可;
如果通过future.whenComplete进行异步回调处理,要注意回调是发生在网络线程中的,此时需要遵守reactive的线程规则。</li>
</ul>
</div>
</div><footer>
<hr/>
<div role="contentinfo">
<!-- Copyright etc -->
</div>
Built with <a href="https://www.mkdocs.org/">MkDocs</a> using a <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<div class="rst-versions" role="note" aria-label="Versions">
<span class="rst-current-version" data-toggle="rst-current-version">
</span>
</div>
<script>var base_url = '..';</script>
<script src="../js/theme_extra.js" defer></script>
<script src="../js/theme.js" defer></script>
<script src="../search/main.js" defer></script>
<script defer>
window.onload = function () {
SphinxRtdTheme.Navigation.enable(true);
};
</script>
</body>
</html>