Error java.io.StreamCorruptedException: invalid stream header: 2D2D2D2D when uploading a WTDocument
Hello, Would you agree to share the whole code of your final solution.
I'm struggling with something similar, I need to POST an upload of a WTDocument and I'm receiving an error: java.io.StreamCorruptedException: invalid stream header: 2D2D2D2D. I'm trying to do it using Java class combine with REST.
Here is the prototype method I'm testing:
public static void uploadOData2(String serviceUrl, String payload, String username, String password, String certPath, String methode, String csrfNonce) {
HttpsURLConnection httpsConn = null;
OutputStream outputStream = null;
try {
// Load the server certificate
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream caInput = new FileInputStream(certPath);
X509Certificate ca = (X509Certificate) cf.generateCertificate(caInput);
caInput.close();
// Create a KeyStore containing the trusted certificate
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
// Create a TrustManager that trusts the certificate in the KeyStore
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keyStore);
// Create an SSLContext that uses the TrustManager
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), null);
File file = new File("c:/temp/test.txt");
if (file.exists()) {
// Set the default SSLContext
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
URL url = new URL(serviceUrl);
httpsConn = (HttpsURLConnection) url.openConnection();
httpsConn.setRequestMethod(methode.toUpperCase());
httpsConn.setRequestProperty("Content-Type", "text/plain");
httpsConn.setRequestProperty("Accept", "text/plain");
httpsConn.setRequestProperty("Content-Disposition", "form-data; name=\"file\"; filename=\"test.txt\"");
httpsConn.setRequestProperty("Content-Length", String.valueOf(file.length()));
httpsConn.setDoOutput(true);
httpsConn.setRequestProperty("CSRF_NONCE", csrfNonce);
DataOutputStream wr = new DataOutputStream(httpsConn.getOutputStream());
FileInputStream fileInputStream = new FileInputStream(file);
byte[] fileContent = fileInputStream.readAllBytes();
fileInputStream.close();
// Append binary content to payload
String boundary = "\r\n---------------------------boundary\r\n";
payload += "\r\n";
payload += fileContent;
payload += boundary;
// Send payload
wr.write(payload.getBytes());
wr.flush();
wr.close();
httpsConn.connect();
int responseCode = httpsConn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
// OData POST request successful
} else {
// OData POST request failed. Server returned response code
InputStream errorStream = httpsConn.getErrorStream();
if (errorStream != null) {
byte[] errorBytes = errorStream.readAllBytes();
// Error response
}
}
httpsConn.disconnect();
} else {
// File not found
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (outputStream != null) outputStream.close();
if (httpsConn != null) httpsConn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}

