阿里云的oss使用URL授权上传文件后,文件内容发生变化
生成临时上传地址的代码如下:
public static URL getUploadUrl(String path, Long userId, String contentType) {
Date expires = DateUtils.addHour(new Date(), 1);
GeneratePresignedUrlRequest request =
new GeneratePresignedUrlRequest(bucketName, path, HttpMethod.PUT);
request.setExpiration(expires);
request.setContentType(contentType);
request.addUserMetadata("author", userId.toString());
URL url = ossClient.generatePresignedUrl(request);
return url;
}
调用URL,实现文件上传的代码如下:
private void post(URL url, File file, String ext, Long userId) throws IOException, URISyntaxException {
HttpClient httpClient= HttpClients.createDefault();
HttpResponse response;
HttpPut put=new HttpPut(url.toURI());
put.addHeader("Content-Type", MimeUtils.getContentType(ext));
put.addHeader("x-oss-meta-author",userId.toString());
InputStream is = new FileInputStream(file);
InputStreamBody body = new InputStreamBody(is, file.getName());
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("file", body)
.build();
put.setEntity(reqEntity);
response=httpClient.execute(put);
HttpEntity resEntity=response.getEntity();
if (resEntity!=null){
String rs = EntityUtils.toString(resEntity, "UTF-8");
logger.info("--------------------------------------");
logger.info("Response content: " + rs);
logger.info("--------------------------------------");
}
EntityUtils.consume(resEntity);
}
我使用该方法上传了一个txt文件,内容为:
1233333
23232233
MimeUtils.getContentType("txt")="text/plain";
在上传成功后通过网页访问,内容变成了:
--Cemr4-Enl8g5d7cSUDJUvYNt7ybMn5FB18n
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
1233333
23232233
--Cemr4-Enl8g5d7cSUDJUvYNt7ybMn5FB18n--
这是什么原因造成的?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论