Hello,
I am opening a connection to my author instance to try to POST an image or any document to my JCR. The values are coming from a form, and I have created my own form action to store the information. I am successful in storing properties and creating CQ Pages, but when it comes to a image/file, I get an error and dont' know where to go from here.
The front-end form is coming from the dispatcher where visitors of the site can submit a form, and it creates the resulting cq:Page where the author verifies it, and then ultimately activiates.
Thank you.
Below is my code snippet:
String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "---------------53ce80d6685a4"; // open a URL connection URL url = new URL("http://localhost:4502/content/myproject/en/test"); // Open a HTTP connection to the URL HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // Allow Inputs conn.setDoInput(true); // Allow Outputs conn.setDoOutput(true); // Don't use a cached copy. conn.setUseCaches(false); String output = ""; output += twoHyphens + boundary + lineEnd; output += "Content-Disposition: form-data; name=\"jcr:primaryType\""; output += lineEnd + lineEnd; output += "cq:Page" + lineEnd; output += twoHyphens + boundary + lineEnd; output += "Content-Disposition: form-data; name=\"jcr:content/jcr:primaryType\""; output += lineEnd + lineEnd; output += "cq:PageContent" + lineEnd; output += twoHyphens + boundary + lineEnd; output += "Content-Disposition: form-data; name=\"jcr:content/jcr:title\""; output += lineEnd + lineEnd; output += "TESTING" + lineEnd; //image post output += twoHyphens + boundary + lineEnd; output += "Content-Disposition: form-data; name=\"jcr:content/image/imageRotate\""; output += lineEnd + lineEnd; output += "0" + lineEnd; output += twoHyphens + boundary + lineEnd; output += "Content-Disposition: form-data; name=\"jcr:content/image/jcr:primaryType\""; output += lineEnd + lineEnd; output += "nt:unstructured" + lineEnd; output += twoHyphens + boundary + lineEnd; output += "Content-Disposition: form-data; name=\"jcr:content/image/file/jcr:primaryType\""; output += lineEnd + lineEnd; output += "nt:file" + lineEnd; output += twoHyphens + boundary + lineEnd; output += "Content-Disposition: form-data; name=\"jcr:content/image/file/jcr:content/jcr:data\"; filename=\"test.png\""; output += lineEnd; output += "Content-Type: image/png" + lineEnd; output += lineEnd; output += request.getParameter("file") + lineEnd; // from the form output += twoHyphens + boundary + twoHyphens + lineEnd; // Use a post method. conn.setRequestMethod("POST"); conn.setRequestProperty("Authorization","Basic " + login); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); conn.setRequestProperty("Content-Length", String.valueOf(output.length()) ); //conn.getOutputStream().write; DataOutputStream dos = new DataOutputStream( conn.getOutputStream()); dos.writeBytes(output); dos.flush(); dos.close(); Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); for (int c; (c = in.read()) >= 0; out.print((char)c));