Hellow folks,
i'm trying to upload a image to a web server. I'm using j2me from the client side and Grails on the server side. The code bellow is related to the j2me:
However, all data "description", "type" and the byte array ("media" field) are null when the server tries to read.Code:... DataOutputStream out = null; public void run() { String boundary = Long.toString(System.currentTimeMillis(), 16); HttpConnection http = (HttpConnection) Connector.open(url); http.setRequestMethod(HttpConnection.POST); http.setRequestProperty("MIME-version", "1.0"); http.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); http.setRequestProperty("Accept", "application/octet-stream" ); http.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0"); out = new DataOutputStream(http.openOutputStream()); //WRITE DATA this.writeField("description", "Image description"); this.writeField("type", "Image"); this.writeFile("media", "image/jpeg", "image.jpg", aByte); this.close(); } public void writeField(String name, String value) { out.write("--".getBytes()); out.write(boundary.getBytes()); out.write("\r\n".getBytes()); // write content header out.write(("Content-Disposition: form-data; name=\"" + name + "\"").getBytes()); out.write("\r\n".getBytes()); out.write("\r\n".getBytes()); // write content out.write(value.getBytes()); out.write("\r\n".getBytes()); } public void writeField(String name, String mimeType, String fileName, byte[] data) { out.write("--".getBytes()); out.write(boundary.getBytes()); out.write("\r\n".getBytes()); out.write(("Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + fileName + "\"").getBytes()); out.write("\r\n".getBytes()); if(mimeType != null) { out.write(("Content-Type: " + mimeType).getBytes()); out.write("\r\n".getBytes()); } out.write(data, 0, data.length); out.write("\r\n".getBytes()); } public void close() throws java.io.IOException { // write final boundary out.write("--".getBytes()); out.write(boundary.getBytes()); out.write("--".getBytes()); out.write("\r\n".getBytes()); out.flush(); out.close(); }
Do someone know why?!!!
Thanks in advance!!!

Reply With Quote

