我正在尝试将我的 Java 代码中的 Base64 编码图像发布到网站。我已经在本地测试了文件的编码和解码,效果很好!但是,当它到达网站时,我被告知图像是空白的。
这是我发布的方式。如果我使用其他操作而不是上传,我会得到正确的响应!
ready = new java.net.URL(url);
WebRequest request = new WebRequest(ready, HttpMethod.POST);
request.setAdditionalHeader("Content-Type", "application/x-www-form-urlencoded");
String requestBody = "action=upload"
+"&key=ABCDEFG123456"
+ "&file=" + encodedFile
+ "&gen_task_id=" + SQL.getNextID();
encodedFile 来自以下代码:
File file = new File("temp.jpg");
FileInputStream fin = new FileInputStream(file);
byte fileContent[] = new byte[(int)file.length()];
fin.read(fileContent);
//all chars in encoded are guaranteed to be 7-bit ASCII
byte[] encoded = Base64.encodeBase64(fileContent);
String encodedFile = new String(encoded);
说真的,我做错了什么??我已经用头撞墙了好几个小时了!