[英]Files uploaded using http PUT are not of exact size on the server
I am trying to upload files of different MIME types to AWS S3 using the presigned URL. 我正在尝试使用预先签名的URL将不同MIME类型的文件上传到AWS S3。 I am able to see the document on S3 but the content is inappropriate. 我可以在S3上看到文档,但是内容不合适。 It is either 0 bytes or less than the expected size for every file that I am trying. 它是0字节或小于我尝试的每个文件的预期大小。
Here is the code, please let me know what is missing: 这是代码,请让我知道缺少的内容:
public static void main(String[] args) {
URLConnection urlconnection = null;
try {
File file = new File(File_Path);
if (isEncoded == "false") {
URL obj = new URL(Upload_Url);
urlconnection = obj.openConnection();
} else {
try {
URI uri = new URI(Upload_Url);
URL url = uri.toURL();
urlconnection = url.openConnection();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
urlconnection.setDoOutput(true);
urlconnection.setDoInput(true);
if (urlconnection instanceof HttpURLConnection) {
((HttpURLConnection) urlconnection).setRequestMethod("PUT");
((HttpURLConnection) urlconnection).setRequestProperty("Content-Type", Content_Type);
((HttpURLConnection) urlconnection).connect();
}
BufferedOutputStream bos = new BufferedOutputStream(urlconnection.getOutputStream());
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
int i;
// read byte by byte until end of stream
while ((i = bis.read()) > 0) {
bos.write(i);
}
bis.close();
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
似乎您的流末检测是错误的,因此当您看到空字节时,您将被截断。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.