@VanVelZ In reference of your topic.
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();
}
}
}
Solved! Go to Solution.
Probably my question unclear.
The used case we have, is a file is generated by a test bench, we want the operator to upload the file into Windchill.
So we created an interface, when he user click it request his username and password then send the file into Windchill and attach it to a wtpart. We wanted to do it using REST api through a java class.
Note that an empty wtdocument is already created before through a Workflow process.
So, here is my solution for this problem in case it can help someone else:
Hi @smartel
This is not case by Windchill or any windchill api methods.
It is general mistake
Check google how to solve the invalid stream header exception
java.io.StreamCorruptedException: invalid stream header
PetrH
Probably my question unclear.
The used case we have, is a file is generated by a test bench, we want the operator to upload the file into Windchill.
So we created an interface, when he user click it request his username and password then send the file into Windchill and attach it to a wtpart. We wanted to do it using REST api through a java class.
Note that an empty wtdocument is already created before through a Workflow process.
So, here is my solution for this problem in case it can help someone else: