写一段上传文件到seaweedfs的java代码
时间: 2023-08-22 08:06:05 浏览: 69
好的,以下是一个上传文件到SeaweedFS的Java代码示例:
```java
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import okhttp3.*;
public class SeaweedFSUploader {
private final String seaweedFSUrl;
private final OkHttpClient client;
public SeaweedFSUploader(String seaweedFSUrl) {
this.seaweedFSUrl = seaweedFSUrl;
this.client = new OkHttpClient();
}
public void uploadFile(File file) throws IOException {
// 创建请求体
RequestBody requestBody = RequestBody.create(
MediaType.parse("application/octet-stream"), file);
// 创建请求
Request request = new Request.Builder()
.url(seaweedFSUrl + "/submit")
.post(requestBody)
.build();
// 发送请求并获取响应
Response response = client.newCall(request).execute();
// 解析响应
String responseBody = response.body().string();
System.out.println("SeaweedFS response: " + responseBody);
response.close();
}
public static void main(String[] args) throws IOException {
String seaweedFSUrl = "http://localhost:8888";
File fileToUpload = new File("path/to/your/file");
SeaweedFSUploader uploader = new SeaweedFSUploader(seaweedFSUrl);
uploader.uploadFile(fileToUpload);
}
}
```
请替换 `seaweedFSUrl` 为您的实际SeaweedFS服务器地址,替换 `fileToUpload` 为您要上传的文件路径。同时请确保您已经添加了OkHttp依赖库。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)