无法将文件存储到具有中文字符(Java)的S3

时间:2016-04-20 22:06:29

标签: java amazon-s3

我需要将一个文件(String)保存到S3,其中包含一些中文字符,并且由于这些字符总是失败,这是我的代码:

# generate output stream with the file 
byte[] bytes = file.getBytes("UTF-8");
ByteArrayOutputStream out = new ByteArrayOutputStream(bytes.length);
out.write(bytes, 0, bytes.length);
byte[] bytes = out.toByteArray();
InputStream stream = new ByteArrayInputStream(bytes);

ObjectMetadata md = new ObjectMetadata();
md.setContentLength(bytes.length);
md.setContentType("text/csv;charset=UTF-8");
md.setContentEncoding("UTF-8");

# save to S3
client.putObject("myBucket", "keyName", stream, md);

我得到的错误:

    com.amazonaws.AmazonClientException: Unable to unmarshall error response (Character reference "&#)
..

Caused by: org.xml.sax.SAXParseException: Character reference "&#
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257) ~[na:1.7.0_79]
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:347) ~[na:1.7.0_79]
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:121) ~[na:1.7.0_79]
    at com.amazonaws.util.XpathUtils.documentFrom(XpathUtils.java:67) ~[aws-java-sdk-1.3.19.jar:na]
    at com.amazonaws.services.s3.internal.S3ErrorResponseHandler.handle(S3ErrorResponseHandler.java:63) ~[aws-java-sdk-1.3.19.jar:na]
    at com.amazonaws.services.s3.internal.S3ErrorResponseHandler.handle(S3ErrorResponseHandler.java:38) ~[aws-java-sdk-1.3.19.jar:na]
    at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:558) ~[aws-java-sdk-1.3.19.jar:na]

0 个答案:

没有答案