mirror of
https://gitee.com/sjqzhang/go-fastdfs.git
synced 2024-12-02 11:17:39 +08:00
864 lines
32 KiB
HTML
864 lines
32 KiB
HTML
|
||
<!DOCTYPE HTML>
|
||
<html lang="" >
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
||
<title>语言"SDK" · GitBook</title>
|
||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||
<meta name="description" content="">
|
||
<meta name="generator" content="GitBook 3.2.3">
|
||
|
||
|
||
|
||
|
||
<link rel="stylesheet" href="gitbook/style.css">
|
||
|
||
|
||
|
||
|
||
<link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
|
||
|
||
|
||
|
||
<link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
|
||
|
||
|
||
|
||
<link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<meta name="HandheldFriendly" content="true"/>
|
||
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
|
||
<meta name="apple-mobile-web-app-capable" content="yes">
|
||
<meta name="apple-mobile-web-app-status-bar-style" content="black">
|
||
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
|
||
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
|
||
|
||
|
||
<link rel="next" href="usage.html" />
|
||
|
||
|
||
<link rel="prev" href="authentication.html" />
|
||
|
||
|
||
</head>
|
||
<body>
|
||
|
||
<div class="book">
|
||
<div class="book-summary">
|
||
|
||
|
||
<div id="book-search-input" role="search">
|
||
<input type="text" placeholder="Type to search" />
|
||
</div>
|
||
|
||
|
||
<nav role="navigation">
|
||
|
||
|
||
|
||
<ul class="summary">
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<li class="chapter " data-level="1.1" data-path="./">
|
||
|
||
<a href="./#vision">
|
||
|
||
|
||
愿景
|
||
|
||
</a>
|
||
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="1.2" data-path="./">
|
||
|
||
<a href="./#background">
|
||
|
||
|
||
背景
|
||
|
||
</a>
|
||
|
||
|
||
|
||
<ul class="articles">
|
||
|
||
|
||
<li class="chapter " data-level="1.2.1" data-path="./">
|
||
|
||
<a href="./#what">
|
||
|
||
|
||
go-fastdfs是什么?
|
||
|
||
</a>
|
||
|
||
|
||
|
||
</li>
|
||
|
||
|
||
</ul>
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="1.3" data-path="./">
|
||
|
||
<a href="./#character">
|
||
|
||
|
||
特性
|
||
|
||
</a>
|
||
|
||
|
||
|
||
<ul class="articles">
|
||
|
||
|
||
<li class="chapter " data-level="1.3.1" data-path="./">
|
||
|
||
<a href="./#advantage">
|
||
|
||
|
||
go-fastdfs优点
|
||
|
||
</a>
|
||
|
||
|
||
|
||
</li>
|
||
|
||
|
||
</ul>
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="1.4" data-path="install.html">
|
||
|
||
<a href="install.html">
|
||
|
||
|
||
安装
|
||
|
||
</a>
|
||
|
||
|
||
|
||
<ul class="articles">
|
||
|
||
|
||
<li class="chapter " data-level="1.4.1" data-path="install.html">
|
||
|
||
<a href="install.html#quick">
|
||
|
||
|
||
极速体验
|
||
|
||
</a>
|
||
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="1.4.2" data-path="install.html">
|
||
|
||
<a href="install.html#docker">
|
||
|
||
|
||
Docker体验
|
||
|
||
</a>
|
||
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="1.4.3" data-path="install.html">
|
||
|
||
<a href="install.html#compiler_install">
|
||
|
||
|
||
编译安装
|
||
|
||
</a>
|
||
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="1.4.4" data-path="install.html">
|
||
|
||
<a href="install.html#small-cluster">
|
||
|
||
|
||
小集群安装
|
||
|
||
</a>
|
||
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="1.4.5" data-path="install.html">
|
||
|
||
<a href="install.html#big-cluster">
|
||
|
||
|
||
海量集群安装(推荐)
|
||
|
||
</a>
|
||
|
||
|
||
|
||
</li>
|
||
|
||
|
||
</ul>
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="1.5" data-path="authentication.html">
|
||
|
||
<a href="authentication.html">
|
||
|
||
|
||
认证
|
||
|
||
</a>
|
||
|
||
|
||
|
||
<ul class="articles">
|
||
|
||
|
||
<li class="chapter " data-level="1.5.1" data-path="authentication.html">
|
||
|
||
<a href="authentication.html#fingerprint">
|
||
|
||
|
||
按文件指纹认证
|
||
|
||
</a>
|
||
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="1.5.2" data-path="authentication.html">
|
||
|
||
<a href="authentication.html#google">
|
||
|
||
|
||
按google认证码认证
|
||
|
||
</a>
|
||
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="1.5.3" data-path="authentication.html">
|
||
|
||
<a href="authentication.html#custom">
|
||
|
||
|
||
用户自定义认证
|
||
|
||
</a>
|
||
|
||
|
||
|
||
</li>
|
||
|
||
|
||
</ul>
|
||
|
||
</li>
|
||
|
||
<li class="chapter active" data-level="1.6" data-path="usage.html">
|
||
|
||
<a href="usage.html">
|
||
|
||
|
||
语言"SDK"
|
||
|
||
</a>
|
||
|
||
|
||
|
||
<ul class="articles">
|
||
|
||
|
||
<li class="chapter " data-level="1.6.1" data-path="usage.html">
|
||
|
||
<a href="usage.html#description">
|
||
|
||
|
||
统一说明(重要)
|
||
|
||
</a>
|
||
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="1.6.2" data-path="usage.html">
|
||
|
||
<a href="usage.html#cmdline">
|
||
|
||
|
||
命令行上传
|
||
|
||
</a>
|
||
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="1.6.3" data-path="usage.html">
|
||
|
||
<a href="usage.html#web">
|
||
|
||
|
||
浏览器上传
|
||
|
||
</a>
|
||
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="1.6.4" data-path="usage.html">
|
||
|
||
<a href="usage.html#python">
|
||
|
||
|
||
Python语言
|
||
|
||
</a>
|
||
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="1.6.5" data-path="usage.html">
|
||
|
||
<a href="usage.html#go">
|
||
|
||
|
||
GO使用
|
||
|
||
</a>
|
||
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="1.6.6" data-path="usage.html">
|
||
|
||
<a href="usage.html#java">
|
||
|
||
|
||
Java语言
|
||
|
||
</a>
|
||
|
||
|
||
|
||
<ul class="articles">
|
||
|
||
|
||
<li class="chapter " data-level="1.6.6.1" data-path="usage.html">
|
||
|
||
<a href="usage.html#java-stream">
|
||
|
||
|
||
Java流上传
|
||
|
||
</a>
|
||
|
||
|
||
|
||
</li>
|
||
|
||
|
||
</ul>
|
||
|
||
</li>
|
||
|
||
|
||
</ul>
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="1.7" data-path="breakpoint.html">
|
||
|
||
<a href="breakpoint.html#go">
|
||
|
||
|
||
断点续传"SDK"
|
||
|
||
</a>
|
||
|
||
|
||
|
||
<ul class="articles">
|
||
|
||
|
||
<li class="chapter " data-level="1.7.1" data-path="breakpoint.html">
|
||
|
||
<a href="breakpoint.html#go">
|
||
|
||
|
||
GO
|
||
|
||
</a>
|
||
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="1.7.2" data-path="breakpoint.html">
|
||
|
||
<a href="breakpoint.html#js">
|
||
|
||
|
||
Javascript
|
||
|
||
</a>
|
||
|
||
|
||
|
||
</li>
|
||
|
||
|
||
</ul>
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="1.8" data-path="api.html">
|
||
|
||
<a href="api.html">
|
||
|
||
|
||
管理API
|
||
|
||
</a>
|
||
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="1.9" data-path="QA.html">
|
||
|
||
<a href="QA.html">
|
||
|
||
|
||
问题汇总
|
||
|
||
</a>
|
||
|
||
|
||
|
||
<ul class="articles">
|
||
|
||
|
||
<li class="chapter " data-level="1.9.1" data-path="QA.html">
|
||
|
||
<a href="QA.html">
|
||
|
||
|
||
常见问题
|
||
|
||
</a>
|
||
|
||
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="1.9.2" data-path="QA.html">
|
||
|
||
<a href="QA.html">
|
||
|
||
|
||
维护问题
|
||
|
||
</a>
|
||
|
||
|
||
|
||
</li>
|
||
|
||
|
||
</ul>
|
||
|
||
</li>
|
||
|
||
<li class="chapter " data-level="1.10" data-path="upgrade.html">
|
||
|
||
<a href="upgrade.html">
|
||
|
||
|
||
更新历史
|
||
|
||
</a>
|
||
|
||
|
||
|
||
</li>
|
||
|
||
|
||
|
||
|
||
<li class="divider"></li>
|
||
|
||
<li>
|
||
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
|
||
Published with GitBook
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
|
||
|
||
</nav>
|
||
|
||
|
||
</div>
|
||
|
||
<div class="book-body">
|
||
|
||
<div class="body-inner">
|
||
|
||
|
||
|
||
<div class="book-header" role="navigation">
|
||
|
||
|
||
<!-- Title -->
|
||
<h1>
|
||
<i class="fa fa-circle-o-notch fa-spin"></i>
|
||
<a href="." >语言"SDK"</a>
|
||
</h1>
|
||
</div>
|
||
|
||
|
||
|
||
|
||
<div class="page-wrapper" tabindex="-1" role="main">
|
||
<div class="page-inner">
|
||
|
||
<div id="book-search-results">
|
||
<div class="search-noresults">
|
||
|
||
<section class="normal markdown-section">
|
||
|
||
<h1 id="description">统一说明(重要)</h1>
|
||
<pre><code>从1.3.3开始,默认开启组(support_group_manage=true)功能(有更好的扩展性)
|
||
访问的url中都会带上group的名称,例如:
|
||
http://10.1.5.9:8080/这里是组名/upload
|
||
如:
|
||
http://10.1.5.9:8080/group1/upload
|
||
如果(support_group_manage=false)
|
||
url变为
|
||
http://10.1.5.9:8080/upload
|
||
</code></pre><h1 id="cmdline">命令上传</h1>
|
||
<p><code>curl -F file=@http-index-fs http://10.1.xx.60:8080/group1/upload</code> </p>
|
||
<h1 id="web">WEB上传(浏览器打开)</h1>
|
||
<p><code>http://yourserver ip:8080/upload.html</code> 注意:不要使用127.0.0.1上传 </p>
|
||
<h2 id="python">python版本: </h2>
|
||
<pre><code class="lang-python"><span class="hljs-keyword">import</span> requests
|
||
url = <span class="hljs-string">'http://10.1.5.9:8080/group1/upload'</span>
|
||
files = {<span class="hljs-string">'file'</span>: open(<span class="hljs-string">'report.xls'</span>, <span class="hljs-string">'rb'</span>)}
|
||
options={<span class="hljs-string">'output'</span>:<span class="hljs-string">'json'</span>,<span class="hljs-string">'path'</span>:<span class="hljs-string">''</span>,<span class="hljs-string">'scene'</span>:<span class="hljs-string">''</span>} <span class="hljs-comment">#参阅浏览器上传的选项</span>
|
||
r = requests.post(url,data=options, files=files)
|
||
print(r.text)
|
||
</code></pre>
|
||
<h2 id="go">golang版本</h2>
|
||
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main
|
||
|
||
<span class="hljs-keyword">import</span> (
|
||
<span class="hljs-string">"fmt"</span>
|
||
<span class="hljs-string">"github.com/astaxie/beego/httplib"</span>
|
||
)
|
||
|
||
<span class="hljs-keyword">func</span> main() {
|
||
<span class="hljs-keyword">var</span> obj <span class="hljs-keyword">interface</span>{}
|
||
req:=httplib.Post(<span class="hljs-string">"http://10.1.5.9:8080/group1/upload"</span>)
|
||
req.PostFile(<span class="hljs-string">"file"</span>,<span class="hljs-string">"filename"</span>)<span class="hljs-comment">//注意不是全路径</span>
|
||
req.Param(<span class="hljs-string">"output"</span>,<span class="hljs-string">"json"</span>)
|
||
req.Param(<span class="hljs-string">"scene"</span>,<span class="hljs-string">""</span>)
|
||
req.Param(<span class="hljs-string">"path"</span>,<span class="hljs-string">""</span>)
|
||
req.ToJSON(&obj)
|
||
fmt.Print(obj)
|
||
}
|
||
</code></pre>
|
||
<h2 id="java">java版本</h2>
|
||
<p>依赖(这里使用了hutool工具包,更简便)</p>
|
||
<pre><code class="lang-xml"><span class="hljs-tag"><<span class="hljs-name">dependency</span>></span>
|
||
<span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>cn.hutool<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span>
|
||
<span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>hutool-all<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span>
|
||
<span class="hljs-tag"><<span class="hljs-name">version</span>></span>4.4.3<span class="hljs-tag"></<span class="hljs-name">version</span>></span>
|
||
<span class="hljs-tag"></<span class="hljs-name">dependency</span>></span>
|
||
</code></pre>
|
||
<p>上传代码</p>
|
||
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
|
||
<span class="hljs-comment">//文件地址</span>
|
||
File file = <span class="hljs-keyword">new</span> File(<span class="hljs-string">"D:\\git\\2.jpg"</span>);
|
||
<span class="hljs-comment">//声明参数集合</span>
|
||
HashMap<String, Object> paramMap = <span class="hljs-keyword">new</span> HashMap<>();
|
||
<span class="hljs-comment">//文件</span>
|
||
paramMap.put(<span class="hljs-string">"file"</span>, file);
|
||
<span class="hljs-comment">//输出</span>
|
||
paramMap.put(<span class="hljs-string">"output"</span>,<span class="hljs-string">"json"</span>);
|
||
<span class="hljs-comment">//自定义路径</span>
|
||
paramMap.put(<span class="hljs-string">"path"</span>,<span class="hljs-string">"image"</span>);
|
||
<span class="hljs-comment">//场景</span>
|
||
paramMap.put(<span class="hljs-string">"scene"</span>,<span class="hljs-string">"image"</span>);
|
||
<span class="hljs-comment">//上传</span>
|
||
String result= HttpUtil.post(<span class="hljs-string">"http://xxxxx:xxxx/group1/upload"</span>, paramMap);
|
||
<span class="hljs-comment">//输出json结果</span>
|
||
System.out.println(result);
|
||
}
|
||
</code></pre>
|
||
<h2 id="java-stream">java版本流式上传</h2>
|
||
<p>Hutool-http、HttpClient、OkHttp3多种方式流式文件上传
|
||
由于有不少人问到上面的问题,现在本人总结了一个常用的几种http客户端文件流式上传的方式,相当于给自己做下记录,同时也给有这方面疑问的朋友一个借鉴。废话不多说,直接上代码吧。代码是基于springboot的maven工程。</p>
|
||
<p>Hutool-http方式
|
||
先在pom中添加hutool的依赖</p>
|
||
<pre><code class="lang-xml"> <span class="hljs-tag"><<span class="hljs-name">dependency</span>></span>
|
||
<span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>cn.hutool<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span>
|
||
<span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>hutool-all<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span>
|
||
<span class="hljs-tag"><<span class="hljs-name">version</span>></span>4.5.1<span class="hljs-tag"></<span class="hljs-name">version</span>></span>
|
||
<span class="hljs-tag"></<span class="hljs-name">dependency</span>></span>
|
||
</code></pre>
|
||
<p>接着在Controller中代码示例</p>
|
||
<pre><code class="lang-java"> <span class="hljs-meta">@RequestMapping</span>(<span class="hljs-string">"/upload"</span>)
|
||
<span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">upload</span><span class="hljs-params">(MultipartFile file)</span> </span>{
|
||
String result = <span class="hljs-string">""</span>;
|
||
<span class="hljs-keyword">try</span> {
|
||
InputStreamResource isr = <span class="hljs-keyword">new</span> InputStreamResource(file.getInputStream(),
|
||
file.getOriginalFilename());
|
||
|
||
Map<String, Object> params = <span class="hljs-keyword">new</span> HashMap<>();
|
||
params.put(<span class="hljs-string">"file"</span>, isr);
|
||
params.put(<span class="hljs-string">"path"</span>, <span class="hljs-string">"86501729"</span>);
|
||
params.put(<span class="hljs-string">"output"</span>, <span class="hljs-string">"json"</span>);
|
||
String resp = HttpUtil.post(UPLOAD_PATH, params);
|
||
Console.log(<span class="hljs-string">"resp: {}"</span>, resp);
|
||
result = resp;
|
||
} <span class="hljs-keyword">catch</span> (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
|
||
<span class="hljs-keyword">return</span> result;
|
||
}
|
||
</code></pre>
|
||
<p>HttpClient方式
|
||
pom依赖</p>
|
||
<pre><code class="lang-xml"><span class="hljs-tag"><<span class="hljs-name">dependency</span>></span>
|
||
<span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>org.apache.httpcomponents<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span>
|
||
<span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>httpclient<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span>
|
||
<span class="hljs-tag"></<span class="hljs-name">dependency</span>></span>
|
||
<span class="hljs-tag"><<span class="hljs-name">dependency</span>></span>
|
||
<span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>org.apache.httpcomponents<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span>
|
||
<span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>httpmime<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span>
|
||
<span class="hljs-tag"></<span class="hljs-name">dependency</span>></span>
|
||
</code></pre>
|
||
<p>接着在Controller中代码示例</p>
|
||
<pre><code class="lang-java"> <span class="hljs-meta">@RequestMapping</span>(<span class="hljs-string">"/upload1"</span>)
|
||
<span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">upload1</span><span class="hljs-params">(MultipartFile file)</span> </span>{
|
||
String result = <span class="hljs-string">""</span>;
|
||
<span class="hljs-keyword">try</span> {
|
||
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
||
CloseableHttpResponse httpResponse = <span class="hljs-keyword">null</span>;
|
||
RequestConfig requestConfig = RequestConfig.custom()
|
||
.setConnectTimeout(<span class="hljs-number">200000</span>)
|
||
.setSocketTimeout(<span class="hljs-number">2000000</span>)
|
||
.build();
|
||
HttpPost httpPost = <span class="hljs-keyword">new</span> HttpPost(UPLOAD_PATH);
|
||
httpPost.setConfig(requestConfig);
|
||
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create()
|
||
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
|
||
.setCharset(Charset.forName(<span class="hljs-string">"UTF-8"</span>))
|
||
.addTextBody(<span class="hljs-string">"output"</span>, <span class="hljs-string">"json"</span>)
|
||
.addBinaryBody(<span class="hljs-string">"file"</span>, file.getInputStream(),
|
||
ContentType.DEFAULT_BINARY, file.getOriginalFilename());
|
||
httpPost.setEntity(multipartEntityBuilder.build());
|
||
httpResponse = httpClient.execute(httpPost);
|
||
|
||
<span class="hljs-keyword">if</span> (httpResponse.getStatusLine().getStatusCode() == <span class="hljs-number">200</span>) {
|
||
String respStr = EntityUtils.toString(httpResponse.getEntity());
|
||
System.out.println(respStr);
|
||
result = respStr;
|
||
}
|
||
|
||
httpClient.close();
|
||
httpResponse.close();
|
||
} <span class="hljs-keyword">catch</span> (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
<span class="hljs-keyword">return</span> result;
|
||
}
|
||
</code></pre>
|
||
<p>OkHttp3上传示例
|
||
pom文件依赖</p>
|
||
<pre><code class="lang-xml"> <span class="hljs-tag"><<span class="hljs-name">dependency</span>></span>
|
||
<span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>com.squareup.okhttp3<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span>
|
||
<span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>okhttp<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span>
|
||
<span class="hljs-tag"><<span class="hljs-name">version</span>></span>3.9.1<span class="hljs-tag"></<span class="hljs-name">version</span>></span>
|
||
<span class="hljs-tag"></<span class="hljs-name">dependency</span>></span>
|
||
</code></pre>
|
||
<p>接着在Controller中代码示例</p>
|
||
<pre><code class="lang-java"> <span class="hljs-meta">@RequestMapping</span>(<span class="hljs-string">"/upload2"</span>)
|
||
<span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">upload2</span><span class="hljs-params">(MultipartFile file)</span> </span>{
|
||
String result = <span class="hljs-string">""</span>;
|
||
<span class="hljs-keyword">try</span> {
|
||
OkHttpClient httpClient = <span class="hljs-keyword">new</span> OkHttpClient();
|
||
MultipartBody multipartBody = <span class="hljs-keyword">new</span> MultipartBody.Builder().
|
||
setType(MultipartBody.FORM)
|
||
.addFormDataPart(<span class="hljs-string">"file"</span>, file.getOriginalFilename(),
|
||
RequestBody.create(MediaType.parse(<span class="hljs-string">"multipart/form-data;charset=utf-8"</span>),
|
||
file.getBytes()))
|
||
.addFormDataPart(<span class="hljs-string">"output"</span>, <span class="hljs-string">"json"</span>)
|
||
.build();
|
||
|
||
Request request = <span class="hljs-keyword">new</span> Request.Builder()
|
||
.url(UPLOAD_PATH)
|
||
.post(multipartBody)
|
||
.build();
|
||
|
||
Response response = httpClient.newCall(request).execute();
|
||
<span class="hljs-keyword">if</span> (response.isSuccessful()) {
|
||
ResponseBody body = response.body();
|
||
<span class="hljs-keyword">if</span> (body != <span class="hljs-keyword">null</span>) {
|
||
result = body.string();
|
||
System.out.println(result);
|
||
}
|
||
}
|
||
} <span class="hljs-keyword">catch</span> (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
|
||
<span class="hljs-keyword">return</span> result;
|
||
}
|
||
</code></pre>
|
||
<p>总结
|
||
上面给出了几个示例,是不是都挺简单的?通过这种方式,就可以在Controller中做中转了,还是挺方便的。顺便提一下,上面几种方式中,我个人觉得Hutool的是最简单的,最方便的,对于HttpClient而言,概念比较多,显得相对复杂,OkHttp也一样,不过比HttpClient显得优雅点。针对一般的并发量,个人觉得hutool的Http已经够用了,底层是基于jdk的HttpUrlConnection实现的。如果对性能有特殊要求的,可以考虑httpclient或者OKHttp,后两者相对而言,更推荐使用OkHttp。</p>
|
||
|
||
|
||
</section>
|
||
|
||
</div>
|
||
<div class="search-results">
|
||
<div class="has-results">
|
||
|
||
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
|
||
<ul class="search-results-list"></ul>
|
||
|
||
</div>
|
||
<div class="no-results">
|
||
|
||
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
</div>
|
||
</div>
|
||
|
||
</div>
|
||
|
||
|
||
|
||
<a href="authentication.html#custom" class="navigation navigation-prev " aria-label="Previous page: 用户自定义认证">
|
||
<i class="fa fa-angle-left"></i>
|
||
</a>
|
||
|
||
|
||
<a href="usage.html#description" class="navigation navigation-next " aria-label="Next page: 统一说明(重要)">
|
||
<i class="fa fa-angle-right"></i>
|
||
</a>
|
||
|
||
|
||
|
||
</div>
|
||
|
||
<script>
|
||
var gitbook = gitbook || [];
|
||
gitbook.push(function() {
|
||
gitbook.page.hasChanged({"page":{"title":"语言\"SDK\"","level":"1.6","depth":1,"next":{"title":"统一说明(重要)","level":"1.6.1","depth":2,"anchor":"#description","path":"usage.md","ref":"usage.md#description","articles":[]},"previous":{"title":"用户自定义认证","level":"1.5.3","depth":2,"anchor":"#custom","path":"authentication.md","ref":"authentication.md#custom","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"usage.md","mtime":"2019-09-24T02:43:29.637Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-10-18T09:52:12.919Z"},"basePath":".","book":{"language":""}});
|
||
});
|
||
</script>
|
||
</div>
|
||
|
||
|
||
<script src="gitbook/gitbook.js"></script>
|
||
<script src="gitbook/theme.js"></script>
|
||
|
||
|
||
<script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
|
||
|
||
|
||
|
||
<script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
|
||
|
||
|
||
|
||
<script src="gitbook/gitbook-plugin-search/search.js"></script>
|
||
|
||
|
||
|
||
<script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
|
||
|
||
|
||
|
||
<script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
|
||
|
||
|
||
|
||
<script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
|
||
|
||
|
||
|
||
<script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
|
||
|
||
|
||
|
||
</body>
|
||
</html>
|
||
|