Reputation: 1
I want to upload a file to a web server. To avoid getting an OutOfMemory error on large files, I added the connection.setChunkedStreamingMode(2048);
method. This works fine when the server responds with a response code of 200. However, when the server responds with an error code like 403 (Access denied), the connection.getResponseCode();
method throws an SocketException, and I am unable to retrieve the response code. The same error is thrown when I try to use connection.getHeaderField(key);
.
My code:
private void uploadTest(File src) {
try {
URL url = new URL("https://localhost:42019/upload");
HttpURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.addRequestProperty("filename", "uploadedFile.txt");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setDoOutput(true);
connection.setChunkedStreamingMode(2048);
try (FileInputStream fis = new FileInputStream(src);
BufferedOutputStream bos = new BufferedOutputStream(connection.getOutputStream())) {
byte[] buffer = new byte[2048];
int count;
while ((count = fis.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
} catch (IOException ignored) {}
responseCode = connection.getResponseCode();
readRCode(connection);
} catch (IOException e) {
e.printStackTrace();
}
}
The error:
java.net.SocketException: Connection reset
at java.base/sun.nio.ch.NioSocketImpl.implRead(NioSocketImpl.java:323)
at java.base/sun.nio.ch.NioSocketImpl.read(NioSocketImpl.java:350)
at java.base/sun.nio.ch.NioSocketImpl$1.read(NioSocketImpl.java:803)
at java.base/java.net.Socket$SocketInputStream.read(Socket.java:976)
at java.base/sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:478)
at java.base/sun.security.ssl.SSLSocketInputRecord.readHeader(SSLSocketInputRecord.java:472)
at java.base/sun.security.ssl.SSLSocketInputRecord.bytesInCompletePacket(SSLSocketInputRecord.java:70)
at java.base/sun.security.ssl.SSLSocketImpl.readApplicationRecord(SSLSocketImpl.java:1460)
at java.base/sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:1064)
at java.base/java.io.BufferedInputStream.fill(BufferedInputStream.java:244)
at java.base/java.io.BufferedInputStream.read1(BufferedInputStream.java:284)
at java.base/java.io.BufferedInputStream.read(BufferedInputStream.java:343)
at java.base/sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:824)
at java.base/sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:759)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1691)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1592)
at java.base/java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:529)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:308)
at de.paull.cloud.Request.uploadTest(Request.java:53)
at de.paull.cloud.Request.main(Request.java:397)
I tried removing the connection.setChunkedStreamingMode(2048);
line, and now I am able to read the response code and the response headers. However, when I attempt to upload a large file, I encounter a java.lang.OutOfMemoryError: Java heap space
error again.
I don't want to switch to another library because I've built my entire project using HttpsURLConnection.
Upvotes: 0
Views: 68