我已经能够成功上传小文件~1kb但是当我尝试上传更大的文件时> 1Mb我得到这个例外:
java.io.IOException: Error writing to server
at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:699)
at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:711)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1585)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:347)
at com.red5.VimeoTechRojoIntegration.uploadFromEFSToS3(VimeoTechRojoIntegration.java:209)
at com.red5.Red5ProLive$1.run(Red5ProLive.java:128)
at java.lang.Thread.run(Thread.java:748)
要上传我正在起诉此代码
OutputStream out = null;
InputStream in = null;
HttpURLConnection connection = null;
try {
URL url = new URL(signedURLS3);
in = new FileInputStream(videoEFSPath);
System.out.println("establish connection");
connection=(HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
connection.setRequestProperty("Content-Type", "application/octet-stream");
System.out.println("get output stream");
out = (OutputStream) connection.getOutputStream();
System.out.println("copy");
IOUtils.copyLarge(in,out);
System.out.println("copy finished");
int responseCode = connection.getResponseCode();
if (responseCode < 300)
return "";
return "{\"error\":\"The upload to S3 failed. AWS server returned response code "+responseCode+"\"}";
}
有什么问题?
答案 0 :(得分:0)
这实际上应该可行,我正在使用Apache HTTP Client:
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.util.EntityUtils;
import org.apache.http.entity.FileEntity;
...
String presignedUrl = ...
File file = ...
HttpPut httpPut = new HttpPut(presignedUrl);
httpPut.setEntity(new FileEntity(file));
HttpResponse httpResponsePut = httpClient.execute(httpPut);
if (httpResponsePut.getStatusLine().getStatusCode() >= HttpStatus.SC_BAD_REQUEST) {
log.error("Error uploading file: " + file.getName() + " / " + httpResponsePut);
}
EntityUtils.consume(httpResponsePut.getEntity());