0

I can see this code

DiskFileUpload fu = new DiskFileUpload();
        // If file size exceeds, a FileUploadException will be thrown
        fu.setSizeMax(1000000);

        List fileItems = fu.parseRequest(request);
        Iterator itr = fileItems.iterator();

        while(itr.hasNext()) {
          FileItem fi = (FileItem)itr.next();

          //Check if not form field so as to only handle the file inputs
          //else condition handles the submit button input
          if(!fi.isFormField()) {
            System.out.println("nNAME: "+fi.getName());
            System.out.println("SIZE: "+fi.getSize());
            //System.out.println(fi.getOutputStream().toString());
            File fNew= new File(application.getRealPath("/"), fi.getName());

            System.out.println(fNew.getAbsolutePath());
            fi.write(fNew);
          }
          else {
            System.out.println("Field ="+fi.getFieldName());
          }
        }

And I am wondering what this code part:

List fileItems = fu.parseRequest(request);
            Iterator itr = fileItems.iterator();

...means for HttpClient? Should I upload file by parts or what does it mean? I want to upload video files with my desktop app but I am not sure how to organize the HttpClient. Please help me to understand.


Client

import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;


public class PostFile {
  public static void main(String[] args) throws Exception {
    HttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

    HttpPost httppost = new HttpPost("http://localhost:8080/uploadtest");
    File file = new File("C:\\file.flv");

    MultipartEntity mpEntity = new MultipartEntity();
    ContentBody cbFile = new FileBody(file, "binary/octet-stream");
    mpEntity.addPart("userfile", cbFile);


    httppost.setEntity(mpEntity);
    System.out.println("executing request " + httppost.getRequestLine());
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity resEntity = response.getEntity();

    System.out.println(response.getStatusLine());
    if (resEntity != null) {
      System.out.println(EntityUtils.toString(resEntity));
    }
    if (resEntity != null) {
      resEntity.consumeContent();
    }

    httpclient.getConnectionManager().shutdown();
  }
}

server

public void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    System.out.println("Content Type ="+request.getContentType());


    try {
      DiskFileUpload fu = new DiskFileUpload();
      // If file size exceeds, a FileUploadException will be thrown
      fu.setSizeMax(1000000);

      List fileItems = fu.parseRequest(request);
      Iterator itr = fileItems.iterator();

      while (itr.hasNext()) {
        FileItem fi = (FileItem) itr.next();

        //Check if not form field so as to only handle the file inputs
        //else condition handles the submit button input
        if (!fi.isFormField()) {
          System.out.println("nNAME: " + fi.getName());
          System.out.println("SIZE: " + fi.getSize());
          //System.out.println(fi.getOutputStream().toString());
          File fNew = new File("D:\\uploaded.flv");

          System.out.println(fNew.getAbsolutePath());
          fi.write(fNew);
        }
        else {
          System.out.println("Field =" + fi.getFieldName());
        }
      }
    }
    catch (Exception ex) {
    }


  }

I want to upload files >=1Gb. What am I doing wrong?

user592704
  • 3,674
  • 11
  • 70
  • 107

1 Answers1

0

No, you don't have to upload your files by parts. In your form, you can have multiple input field of the type "file".

List fileItems = fu.parseRequest(request);

The above code returns you the list of all the "file" input field in your request. So, if you have two file fields, you get two FileItem with its content. The next statement:

Iterator itr = fileItems.iterator();

is used to get the iterator and iterate the list of FileItem you just extracted from your request. Remeber, each FileItem object is a file that you uploaded.

Abdel Raoof Olakara
  • 19,223
  • 11
  • 88
  • 133
  • mm.. that was not a comment.. it was an answer. Did you get what you are looking for? – Abdel Raoof Olakara Mar 14 '11 at 06:01
  • I want to upload video files (>=1Gb) with HttpClient and FileUpload but I cannot find a really useful code examples. desktop->servlet file upload. Is there a standart tutorial for this task? – user592704 Mar 14 '11 at 06:04
  • I mentioned item and parts because I maybe missed them with chunks HttpUrlConnection code way :) But there is a max_chunk_size limit so I cannot send video files with sun's UrlConnection :( That's why I put my attention on HttpClient and FileUpload libs but I cannot find apache video upload code examples :( – user592704 Mar 14 '11 at 06:12
  • well, you didn't mention anything about 1GB files in your question.. First, what is the issue that you are facing? do you get any error, time out? – Abdel Raoof Olakara Mar 14 '11 at 06:12
  • Second, the fu.setSizeMax(1000000) should be something like 1024*1024*1024.. the will set 1GB as the max. If you need more than that you will have to change it – Abdel Raoof Olakara Mar 14 '11 at 06:13
  • I use this client code http://stackoverflow.com/questions/1067655/how-to-upload-a-file-using-java-httpclient-library-working-with-php-strange-pro (adapted for servlet) and this server (Listing 9-5) http://www.theserverside.com/news/1365153/HttpClient-and-FileUpload But no file get uploaded :( How should it be adapted? I'd like to see some tips – user592704 Mar 14 '11 at 06:16
  • Since you are handling large files, why don't you try FTP? – Abdel Raoof Olakara Mar 14 '11 at 06:19
  • I'd love to but I cannot do this ...http only :( Is it hopeless? – user592704 Mar 14 '11 at 06:20
  • No, all goes OK but no files at D:\\ appears? – user592704 Mar 14 '11 at 06:27
  • Ok... I want to demo the code here. Please watch it because I have less experience. – user592704 Mar 14 '11 at 06:28