【问题标题】:FileUpload and HttpClient upload items and partsFileUpload 和 HttpClient 上传项目和部分
【发布时间】:2011-03-14 05:10:51
【问题描述】:

我可以看到这段代码

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());
          }
        }

我想知道这段代码是什么部分:

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

...表示 HttpClient?我应该分部分上传文件还是什么意思?我想用我的桌面应用程序上传视频文件,但我不确定如何组织 HttpClient。 请帮我理解。


客户

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();
  }
}

服务器

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) {
    }


  }

我想上传文件 >=1Gb。我做错了什么?

【问题讨论】:

    标签: java file-io upload


    【解决方案1】:

    不,您不必分段上传文件。在您的表单中,您可以有多个“文件”类型的输入字段。

    List fileItems = fu.parseRequest(request);
    

    上面的代码返回您请求中所有“文件”输入字段的列表。因此,如果您有两个文件字段,您将获得两个 FileItem 及其内容。下一条语句:

    Iterator itr = fileItems.iterator();
    

    用于获取迭代器并迭代您刚刚从请求中提取的 FileItem 列表。请记住,每个 FileItem 对象都是您上传的文件。

    【讨论】:

    • mm.. 这不是评论.. 这是一个答案。你得到你想要的东西了吗?
    • 我想用 HttpClient 和 FileUpload 上传视频文件 (>=1Gb) 但我找不到真正有用的代码示例。桌面-> servlet 文件上传。这个任务有标准教程吗?
    • 我提到了项目和部分,因为我可能用块 HttpUrlConnection 代码方式错过了它们 :) 但是有一个 max_chunk_size 限制,所以我不能用 sun 的 UrlConnection 发送视频文件 :( 这就是我把注意力放在 HttpClient 上的原因和 FileUpload 库,但我找不到 apache 视频上传代码示例 :(
    • 好吧,您在问题中没有提到任何有关 1GB 文件的内容。首先,您面临的问题是什么?你有任何错误,超时吗?
    • 其次,fu.setSizeMax(1000000) 应该是 1024*1024*1024.. 将设置 1GB 作为最大值。如果你需要更多,你将不得不改变它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多