Hello all,
I've been having some problems trying to use HttpClient to upload files.
My platform:
Client: Stand-alone Java client, httpclient: 3.0.1
Server: Weblogic 9.1, commons-fileupload 1.1.1
When I execute the code I attached below (trimmed a bit for space),
which appears to be correct per the tutorial, I receive the following
error:
"Upload failed: org.apache.commons.fileupload.FileUploadException: the
request was rejected because no multipart boundary was found"
If I don't manually set the Content-type or Content-length, I get
corresponding errors as well. Strangely, if I use the deprecated
MultipartPostMethod, I do not receive the error above and the upload
works for very small files. However, for any reasonably sized file
(>1KB), I receive the following exception on the client side:
java.net.SocketException: Software caused connection abort: socket write
error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at java.io.BufferedOutputStream.write(BufferedOutputStream.java:105)
at
org.apache.commons.httpclient.WireLogOutputStream.write(WireLogOutputStr
eam.java:67)
at
org.apache.commons.httpclient.methods.multipart.FilePart.sendData(FilePa
rt.java:222)
at
org.apache.commons.httpclient.methods.multipart.Part.send(Part.java:311)
at
org.apache.commons.httpclient.methods.multipart.Part.sendParts(Part.java
:384)
at
org.apache.commons.httpclient.methods.multipart.Part.sendParts(Part.java
:358)
at
org.apache.commons.httpclient.methods.MultipartPostMethod.writeRequestBo
dy(MultipartPostMethod.java:298)
I'm completely at a loss here...any ideas?
Thanks,
Jeff
public static void main(String[] args) {
....
PostMethod post = new PostMethod(servletPath);
try {
File tempFile = new File("C:\\0.txt");
List<Part> parts = new ArrayList<Part>();
parts.add(new FilePart("file", tempFile.getName(), tempFile));
MultipartRequestEntity multipartRequestEntity = new
MultipartRequestEntity(parts.toArray(new Part[0]), post.getParams());
post.setRequestEntity(multipartRequestEntity);
post.setContentChunked(true);
post.setRequestHeader("Content-type", "multipart/form-data");
post.setRequestHeader("Content-length", "" +
multipartRequestEntity.getContentLength());
// add parameters
post.addParameter("description", "desc");
...
post.setDoAuthentication(true);
HttpClient client = new HttpClient();
client.getState().setCredentials(
new AuthScope(host, Integer.parseInt(port)),
new UsernamePasswordCredentials(user, password));
int status = client.executeMethod(post);
if (HttpStatus.SC_OK != status) {
String response = post.getResponseBodyAsString();
System.out.println(attachmentGuid);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
post.releaseConnection();
}
}