- 有谁有java调用云开发HTTP api上传文件的代码样例?
我在用java调用云开发HTTPAPI上传文件时,总返回400错误,麻烦大神帮忙看下问题出在哪? import cn.hutool.json.JSONObject; import com.dimon.cloud.commons.rest.ApiResponse; import com.dimon.cloud.customize.feign.WechatApi; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.HttpClientBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import javax.validation.constraints.NotNull; import java.io.IOException; /** * @Author: He Zhigang * @Date: 2019/10/12 18:00 * @Description: */ @RestController public class BackendController { @Autowired String accessToken; @Autowired WechatApi wechatApi; @PostMapping("/upload/prod/img") public ApiResponse uploadFile(@NotNull MultipartFile file) throws IOException { String dir = "p_cust/prod/img/"; String path = dir + file.getOriginalFilename(); JSONObject reqBody = new JSONObject(); reqBody.put("env", "dev-4cs4fs"); reqBody.put("path", path); JSONObject respJson = wechatApi.uploadFile(accessToken, reqBody); String url = respJson.getStr("url"); // HttpResponse httpResponse = HttpRequest.post(url) // .contentType("multipart/form-data") // .header("Content-Type", "multipart/form-data") // .form("key", path) // .form("Signature", respJson.getStr("authorization")) // .form("x-cos-security-token", respJson.getStr("token")) // .form("x-cos-meta-fileid", respJson.getStr("cos_file_id")) // .form("file", file) // .execute(); HttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost(url); httpPost.setHeader("Content-Type", "multipart/form-data"); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addBinaryBody("file", file.getBytes()) .addTextBody("key", path) .addTextBody("Signature", respJson.getStr("authorization")) .addTextBody("x-cos-security-token", respJson.getStr("token")) .addTextBody("x-cos-meta-fileid", respJson.getStr("cos_file_id")); httpPost.setEntity(builder.build()); org.apache.http.HttpResponse res = httpClient.execute(httpPost); return ApiResponse.ok().setBody(res); } } 上面我用了两种不同的HTTP client 都是返回同样的错误。[图片]
2019-10-23 - 真机调试和开发工具调试得到的云数据库返回结果不一致
- 当前 Bug 的表现(可附上截图) - 预期表现 - 复现路径 - 提供一个最简复现 Demo [代码]// 是否可以提交[代码][代码] [代码][代码]if[代码] [代码](submitEnable) {[代码][代码] [代码][代码]this[代码][代码].data.test.finish = [代码][代码]true[代码][代码];[代码][代码] [代码][代码]// 根据_openid 和 testId查询是否存在测试记录[代码][代码] [代码][代码]mytest.where({[代码][代码] [代码][代码]_openid: global.openid,[代码][代码] [代码][代码]testId: [代码][代码]this[代码][代码].data.test.testId[代码][代码] [代码][代码]}).get({[代码][代码] [代码][代码]success: res => {[代码][代码] [代码][代码]if[代码] [代码](res.data.length > 0) {[代码][代码] [代码][代码]delete[代码] [代码]this[代码][代码].data.test._openid[代码][代码] [代码][代码]delete[代码] [代码]this[代码][代码].data.test._id[代码][代码] [代码][代码]mytest.doc(res.data[0]._id).set({[代码][代码] [代码][代码]data: [代码][代码]this[代码][代码].data.test[代码][代码] [代码][代码]}).then(res => {[代码][代码] [代码][代码]console.log([代码][代码]"mytestId:======"[代码] [代码]+ res._id);[代码][代码] [代码][代码]console.log(res);[代码][代码] [代码][代码]wx.navigateTo({[代码][代码] [代码][代码]url: [代码][代码]'/pages/result/result?id='[代码] [代码]+ res._id[代码][代码] [代码][代码]})[代码][代码] [代码][代码]})[代码][代码] [代码][代码]} [代码][代码]else[代码] [代码]{[代码][代码] [代码][代码]mytest.add({[代码][代码] [代码][代码]data: [代码][代码]this[代码][代码].data.test[代码][代码] [代码][代码]}).then(res => {[代码][代码] [代码][代码]wx.navigateTo({[代码][代码] [代码][代码]url: [代码][代码]'/pages/result/result?id='[代码] [代码]+ res._id[代码][代码] [代码][代码]})[代码][代码] [代码][代码]})[代码][代码] [代码][代码]}[代码][代码] [代码][代码]}[代码][代码] [代码][代码]});[代码][代码] [代码] [代码] [代码][代码]}[代码] [图片] 上面这个是本地调试更新某个数据返回的结果,_id有值 下面这个是真机调试更新某个数据返回的结果,_id为""? [图片]
2019-06-21