我正在尝试使用J2ME代码通过Amazon REST API(POST对象)将文件上传到Amazon S3。这是我的代码:
String url = "http://xxxxxxxxx.s3.amazonaws.com";
//open connection
hc = (HttpConnection) Connector.open(url,Connector.READ_WRITE);
//set Post data
hc.setRequestMethod(HttpConnection.POST);
//set postvalues
hc.setRequestProperty("Content-Type", "multipart/form-data; boundary=-----1234567890");
hc.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Confirguration/CLDC-1.0");
hc.setRequestProperty("Content-Language", "en-US");
//hc.setRequestProperty("acl", "public-read");
//hc.setRequestProperty("Accept", "text/plain");
hc.setRequestProperty("Keep-Alive", "300");
hc.setRequestProperty("Connection", "keep-alive");
hc.setRequestProperty("AWSAccessKeyId", "XXXXXXXXXXXXXXX");
hc.setRequestProperty("key", "XXXXXXXXXXXXXXX");
//hc.setRequestProperty("file", "contactss.vcf");
//hc.setRequestProperty("submit", "asdasdasdasd");
hc.setRequestProperty("Content-Length", data.length() + ("-----1234567890".getBytes().length * 2) + "");
hc.setRequestProperty("Content-length", data.length() + ("-----1234567890".getBytes().length * 2) + "");
DataOutputStream dos = hc.openDataOutputStream();
//dos.write("-----1234567890".getBytes());
dos.write(data.getBytes());
执行后。我收到以下回复:
<Error><Code>InvalidArgument</Code><Message>POST requires exactly one file upload per request.</Message><ArgumentValue>0</ArgumentValue><ArgumentName>file</ArgumentName><RequestId>686AB7E3C10779B9</RequestId><HostId>75OKRHQ59yfJLZ8nThsNE8G7PukbBdnoL7lonZX6EprPpqik5pAcKOK/TG9tGBdA</HostId></Error>
但该文件未上传到Amazon S3。请帮我解决。
答案 0 :(得分:0)
看起来你的文件行被注释掉了,你甚至没有发送文件。
...
hc.setRequestProperty("key", "XXXXXXXXXXXXXXX");
//hc.setRequestProperty("file", "contactss.vcf");
//hc.setRequestProperty("submit", "asdasdasdasd");
hc.setRequestProperty("Content-Length", data.length() + ("------1234567890".getBytes().length * 2) + "");
...
话虽如此,我强烈建议您使用AWS Java SDK: