请求方式
POST https://cepmtest.dhcccloud.com.cn/upload2cmp/
添加请求头Content-Type:multipart/form-data;
| 请求参数 | 类型 | 是否必填 | 说明 | | --- | --- | --- | --- | --- | | 无 | -| - | - |
| 返回参数 | 类型 | 是否比返 | 说明 |
| --- | --- | --- | --- | --- |
| errcode | Num| 是 | 0交易成功
其它为异常情况 |
| errmsg | String | 否 | errcode不为0时有值 |
| data | String | 否 | 成功时返回文件存储路径,供其它接口使用 |
public static String uploadMultiFile(String path) {
String url ="https://cepmtest.dhcccloud.com.cn/upload2cmp/";
File file = new File(path);
MediaType mediaType=MediaType.Companion.parse("text/x-markdown; charset=utf-8");
RequestBody fileBody=RequestBody.Companion.create(file,mediaType);
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", file.getName().toLowerCase(), fileBody)
.build();
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
Response response = null;
try {
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
response = okHttpClient.newCall(request).execute();
if (response.code() == 200) {
logger.info("uploadMultiFile; [postUrl="+url+", file="+path+", responseCode="+response.code()+"]");
return response.body().string();
} else {
logger.warning("uploadMultiFile请求失败,[url="+url+", file="+path+"]");
}
} catch (IOException e) {
logger.warning("uploadMultiFile请求失败,[url="+url+", file="+path+"]");
e.printStackTrace();
throw new RuntimeException("uploadMultiFile请求失败,url:" + url);
}
return null;
}
var formData = new FormData();
formData.append("file", file);
$.ajax({
url: 'https://cepmtest.dhcccloud.com.cn/upload2cmp/',
dataType: 'json',
type: 'POST',
async: true,
data: formData,
processData: false, // 使数据不做处理
contentType: false, // 不要设置Content-Type请求头
success: function (data) {
console.log(data);
},
error: response => {
console.error(response);
}
});