我正嘗試使用預先設置的URL將不同MIME類型的文件上傳到AWS S3。我能夠在S3上看到文檔,但內容不恰當。它要麼是0字節,要麼小於我嘗試的每個文件的預期大小。使用http PUT上傳的文件在服務器上的大小不正確
下面是代碼,請讓我知道什麼是缺少:
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();
}
它是否符合內容或尺寸?有什麼異常? – Lokesh
什麼HTTP狀態碼被返回? – EJP
文件大小不合適。因此,當我下載文件時,它已損壞。 – Jayashree