我是堆栈溢出的新手。希望我正确地提出问题。
我必须在Java中转换下面的命令。有了这个,我还必须在 multipart / form-data 中将位图上传到服务器。我尝试使用http urlconnection,但我不断收到400 Bad Request错误。我不确定curl命令中使用的语法以及如何在java中进行转换。任何帮助将不胜感激。
curl -X POST 'https://my.craftar.net/api/v0/image/?api_key=123456789abcdefghijk123456789abcdefghijk'
-F "item=/api/v0/item/4fe672886ec142f6ab6d72d54acf046f/"
-F "file=@back_cover.jpg"
您可以查看链接here以获取任何说明。
这是我迄今为止所尝试过的:
private String webAddressToPost = "https://my.craftar.net/api/v0/image/?api_key=123456789abcdefghijk123456789abcdefghijk";
URL url = new URL(webAddressToPost);
HttpURLConnection conn = NetCipher.getHttpsURLConnection(url);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data");
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100, bos);
byte[] data = bos.toByteArray();
ByteArrayBody bab = new ByteArrayBody(data, "test.png");
entity.addPart("item",new StringBody(ITEM_ID);
entity.addPart("file", bab);
conn.addRequestProperty("Content-length", entity.getContentLength() + "");
答案 0 :(得分:0)
参见此示例
https://github.com/square/okhttp/wiki/Recipes#posting-a-multipart-request
class Login:
def __init__(self, user, password):
self.user = user
self.password = password
def as_tuple(self):
return self.user, self.password
login_details = Login(user='a', password='b')
print(login_details.as_tuple())
>> ('a', 'b')