1.maven
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.9.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
2.代码
2.1 GoFastUtil.java
package com.gofastdfs.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
public class GoFastUtil {
public static GoFastFileResult upload(String url, String scene, String fileName, byte[] bytes) throws Exception {
MultipartBody multipartBody = new MultipartBody.Builder().
setType(MultipartBody.FORM)
.addFormDataPart("file", fileName, RequestBody.create(MediaType.parse("multipart/form-data;charset=utf-8"), bytes))
.addFormDataPart("output", "json")
.addFormDataPart("scene", scene)
.build();
return getUpload(url, multipartBody);
}
private static GoFastFileResult getUpload(String url, MultipartBody multipartBody) throws Exception {
GoFastFileResult goFastFileResult = new GoFastFileResult();
try {
Request request = new Request.Builder().url(url).post(multipartBody).build();
Response response = new OkHttpClient().newCall(request).execute();
if (response.isSuccessful()) {
ResponseBody body = response.body();
if (body != null) {
String result = body.string();
System.out.println(result);
JSONObject jsonObject = JSON.parseObject(result);
if (String.valueOf(jsonObject.get("status")).equals("fail")) {
throw new Exception("上传文件异常:" + jsonObject.get("message"));
} else {
goFastFileResult.setUrl(String.valueOf(jsonObject.get("url")));
goFastFileResult.setMd5(String.valueOf(jsonObject.get("md5")));
goFastFileResult.setPath(String.valueOf(jsonObject.get("path")));
goFastFileResult.setDomain(String.valueOf(jsonObject.get("domain")));
goFastFileResult.setScene(String.valueOf(jsonObject.get("scene")));
goFastFileResult.setSize(Long.parseLong(String.valueOf(jsonObject.get("size"))));
goFastFileResult.setMtime(Long.parseLong(String.valueOf(jsonObject.get("mtime"))));
goFastFileResult.setScenes(String.valueOf(jsonObject.get("scenes")));
goFastFileResult.setRetmsg(String.valueOf(jsonObject.get("retmsg")));
goFastFileResult.setRetcode(String.valueOf(jsonObject.get("retcode")).equals("0"));
goFastFileResult.setSrc(String.valueOf(jsonObject.get("src")));
}
}
}
} catch (Exception e) {
System.out.println("上传文件到影像服务器异常:" + e);
throw new Exception("上传文件异常", e);
}
return goFastFileResult;
}
}
2.2 GoFastFileResult.java
package com.gofastdfs.utils;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;
import java.io.Serializable;
@Data
@Accessors(chain = true)
@ToString
public class GoFastFileResult implements Serializable {
private String url;
private String md5;
private String path;
private String domain;
private String scene;
private long size;
private long mtime;
private String scenes;
private String retmsg;
private Boolean retcode;
private String src;
}