views:

49

answers:

1

Hi to all I have a sample code using Jakarta HttpClient that uploads a file to a web server. What i want is to simulate a multiple file upload of the same file with different name for each upload. Is this possible? Any hints?

A.K.

+2  A: 

Just add different multipart parts with same file content but a different part and filename. With InputStreamBody you can specify a different filename for each part. E.g.

MultipartEntity entity = new MultipartEntity();
entity.addPart("file1", new InputStreamBody(new FileInputStream(file), "name1.ext"));
entity.addPart("file2", new InputStreamBody(new FileInputStream(file), "name2.ext"));
entity.addPart("file3", new InputStreamBody(new FileInputStream(file), "name3.ext"));
// ...

In the Servlet code, assuming that you're using Commons FileUpload, you could just iterate over the multipart items which you've extracted from the request with help of the FileUpload API in a for loop.

List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
    if (item.isFormField()) {
        // Process regular field.
    } else {
        // Process uploaded file.
    }
}
BalusC
+1 12 more to go..
org.life.java
How the InputStreamBody can be written in HttpClient 3.x?
Antonis
I see that you already asked [another question](http://stackoverflow.com/questions/3926391/inputstreambody-equivalent-in-httpclient-3-x) about that :)
BalusC
Yes. Your answer is perfectly right, but with HttpClient 3 i can't make it work!
Antonis