如图所示,本来是一个普通的png图片,上传到OSS后,下载下来发现没法打开(被告示文件格式错误或损坏),用记事本打开来后发现png文件开头和结尾多了一串东西!
开头:
--KlX5FEQvcWEgCzcL_vqgu4oRwn-5PO5PuLLF
结尾:
Content-Disposition: form-data; name="birdy.png"; filename="birdy.png"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
--KlX5FEQvcWEgCzcL_vqgu4oRwn-5PO5PuLLF--
请问这些是什么?为什么上传的时候会被加入进去?
-------------------------
-------------------------
-------------------------
-------------------------
//初始化OSSClient
OSSClient client = new OSSClient(accessKeyId, accessKeySecret);
//GeneratePresignedUrl
GeneratePresignedUrlRequest gpur = new GeneratePresignedUrlRequest(bucketName, "birdy.png", HttpMethod.PUT);
gpur.setExpiration(new Date(new Date().getTime() + 1000*60));
gpur.setContentType("png");
URL url = client.generatePresignedUrl(gpur);
String urlRst = url.toString();
//模仿上传过程
HttpClient httpclient = new DefaultHttpClient();
HttpPut httpPut = new HttpPut(urlRst);
File file = new File("D:\\temp\\birdy.png");
FileBody bin = new FileBody(file);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("birdy.png" , bin);
httpPut.setEntity(reqEntity);
httpPut.addHeader("Content-Type", "png");
HttpResponse rsps = httpclient.execute(httpPut);
int statusCode = rsps.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity resEntity = rsps.getEntity();
String rst = EntityUtils.toString(resEntity);
System.out.println(rst);
response.getWriter().write(rst);
response.getWriter().close();
EntityUtils.consume(resEntity);
System.out.println("Upload Success!!");
}else{
HttpEntity resEntity = rsps.getEntity();
String rst = EntityUtils.toString(resEntity);
System.out.println(rst);
response.getWriter().write(rst);
response.getWriter().close();
EntityUtils.consume(resEntity);
}
用的是HTTPClient,我记得上个月这个帖子
http://bbs.aliyun.com/read/187516.html?spm=0.0.0.0.dX43Jk就是你回复我的哈,这次又麻烦你了!
-------------------------
-------------------------
-------------------------
-------------------------
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。