Hello All, I have the problem with uploading images using the PostMethod of commons HTTPClient (3.0.1). The method is used to upload a .jpg image to one external site along with posting other form text fields. However, I got the error message returned from the server when I excute the following code - "Files not uploaded. We only accept .jpg, or gif". Strangely, the same code has been used to upload the images to other external sites sucessfully. Any suggestion why is this happening? Thanks.
// HTML form <form name="newlist" action=" http://www.domainname.com/index.cfm?fuseaction=newlist" enctype="multipart/form-data" method="POST" enablecab="NO"> <input type="text name="text-field"> <input type="file" name="photofile" value="" accept="image/gif,image/jpg"> <input type="image" src="/upload.gif" alt="upload image" onClick=" document.newlist.submit();"> </form> // posting code public static void main(String[] args) { .... PostMethod lPostMethod = new PostMethod("http://www.domainname.com/index.cfm?fuseaction=newlist"); List lParts = new ArrayList(); // add string part lParts.add(new StringPart("text-field", "abc")); // add file part try { File lImageFile = new File("C:\\heart.jpg"); FilePart lFilePart = new FilePart("photofile", lFile); lFilePart.setContentType("image/jpeg"); lParts.add(lFilePart); MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(lParts.toArray(new Part[0]), lPostMethod.getParams()); lPostMethod.setRequestEntity(multipartRequestEntity); HttpClient client = new HttpClient(); int status = client.executeMethod(lPostMethod); if (status == 200) { String response = post.getResponseBodyAsString(); System.out.println(attachmentGuid); } } catch (IOException e) { e.printStackTrace(); } finally { post.releaseConnection(); } }
