注意事项
输入参数
参数 |
说明 |
类型 |
长度(字节) |
是否必填 |
- |
- |
- |
- |
- |
调用示例
java:
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;
}
javascript:
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);
}
});