1

我已经能够成功上传 ~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+"\"}";   
}

问题是什么?

4

1 回答 1

0

这实际上应该有效,我为此使用了 Apache HTTP 客户端:

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());
于 2018-04-20T06:34:49.163 回答