2011-03-14 165 views
3

我想用HttpClient將大型視頻文件(> = 1Gb)上載到servlet,但找不到任何實例。 你能給我一些有用的代碼示例嗎?使用HttpClient上傳大文件


我想看到HttpClient和FileUpload演示代碼(作爲一個項目)。我可以使用FileUpload lib來下載大文件,這非常有趣嗎?


有趣的是,有上傳的約束這樣的小程序 - > servlet的

回答

1

這裏的HttpClient和文件上傳的例子:

http://www.theserverside.com/news/1365153/HttpClient-and-FileUpload

我不知道你的意思通過「真實」。這足夠真實,即使它不符合你的情況。

1GB?您可能會遇到文件大小限制問題。在本地嘗試,看看。如果失敗了,至少你會有一些真實的代碼和更確切的條件在這裏發佈。

+0

感謝您對這樣一個快速回復 我發現眼前這個例子 – user592704 2011-03-14 00:59:51

+0

9-6。 HttpMultiPartFileUpload.java – user592704 2011-03-14 01:00:26

+0

這很有趣,但我沒有看到任何提到的塊或緩衝區?所以HttpClient沒有HttpUrlConnection max_chunk_size限制? – user592704 2011-03-14 01:03:13

1

我可以上傳大於200mb的大文件,請查看jsp下面的spring代碼。

Jsp code : 
<script src="../lib/app/configurator.data.ajax.js" type="text/javascript"></script> 
<script src="../lib/ui/jquery.fileupload.js"></script> 
<html> 

<script language="Javascript"> 
var varb = ''; 
var test = 0; 
var count = 1; 
$(function() { 
var maxChunkSize = 30000000; //SET CHUNK SIZE HERE 
var your_global_var_for_form_submit = ''; 

var params = { 
     year: threeDSelectedYear, 
     series: threeDSelectedSeries 
}; 
/*File upload bind with form, 'fileupload' is my form id. As sumit triggers 
    for file ulaod will submit my form*/ 
/* replaceFileInput: true, */ 
$('#fileupload').fileupload({ 
maxChunkSize: maxChunkSize, 
url: efccustom.getEfcContext()+'upload/uploadZip?year='+threeDSelectedYear+'&series='+threeDSelectedSeries,    //URL WHERE FILE TO BE UPLOADED 
    error: function (jqXHR, textStatus, errorThrown) { 
    // Called for each failed chunk upload 
     $(".fileupload-loading").html(""); 
     $('.ANALYZE_DIALOG').dialog("close");  
    }, 

    success: function (data, textStatus, jqXHR) { 
    /*This event will be called on success of upload*/ 
    count = parseInt($('#counter').val()) + 1; 
    $('#counter').val(count); 

    $('#ttk').val(count); 
    data.ttk = 7; 
    console.log(" count ",count , data); 
    }, 

    submit: function (e, data) { 
    /*This event will be called on submit here i am 
        adding variable to form*/ 
    //console.log($('#zip_uploaded_file').val());  

    //console.log(data.originalFiles[0].name);    
    test = data.originalFiles[0]; 
    $('#fname').val(data.originalFiles[0].name); 
    $('#trequests').val(Math.ceil(data.originalFiles[0].size/maxChunkSize)); 
    $('#counter').val('1'); 
    }, 

    progress: function (e, data) { 
    /*PROGRESS BAR CODE WILL BE HERE */ 
     $(".fileupload-loading").html('<img src="../public/themeroller/images/throbber.gif" alt="Uploading Please Wait..." title="Uploading Please Wait...." />');  
    }, 

    add: function (e, data) { 
    $('.browsedFileName').html(data.originalFiles[0].name); 
    your_global_var_for_form_submit = data; 
    }, 

    done: function (e, data) { 

     ajaxcall.Data._get('upload/extractZipNCreateJson',params,function(data2) { 
      alert("file upload success "); 
      $(".fileupload-loading").html(""); 
      $('.ANALYZE_DIALOG').dialog("close"); 
     }); 


    } 

}); 
/*This is my button click event on which i am submitting my form*/ 
    $('#button').click(function(){ 
    your_global_var_for_form_submit.submit(); 
    }); 
}); 
</script> 

<html> 

<body> 


<form id="fileupload" enctype="multipart/form-data"> 
<div class="row fileupload-buttonbar"> 
<div class="span7"> 
<!--<input type="file" name="files" id="file" /> --> 

<input type="file" id="zip_uploaded_file" name="zip_uploaded_file" /> 
<input type="hidden" name="counter" id="counter" value="1" /> 
<input type="hidden" name="fname" id="fname" value="" /> 
<input type="hidden" name="trequests" id="trequests" value="1" /> 

<input type="hidden" name="ttk" id="ttk" value="1" /> 
<input type="button" id="button" name="button" value="submit" /> 
<span class='browsedFileName'></span> 
</div> 
</div> 
<!-- The loading indicator is shown during file processing --> 
<div class="fileupload-loading"></div> 
</form> 
</body> 


Controller COde : 
@RequestMapping(value = "/uploadZip", method = RequestMethod.POST, consumes = "multipart/form-data") 
    @ResponseBody 
    public ResponseEntity<String> 
    uploadZip(HttpServletRequest request, HttpServletResponse response) 
      throws IOException, IllegalArgumentException { 
     try {  
     String year = request.getParameter("year"); 
     String series = request.getParameter("series"); 
     log.info(" year " + year + " series " + series); 
     boolean isMultipart = ServletFileUpload.isMultipartContent(request); 
     if (isMultipart) { 

      FileItemFactory factory = new DiskFileItemFactory(); 
      ServletFileUpload upload = new ServletFileUpload(factory); 
      List items = upload.parseRequest(request); 
      Iterator iterator = items.iterator(); 

      HttpSession session = request.getSession(); 
      UserContext userContext = (UserContext) session 
        .getAttribute(ApplicationConstant.USER_CONTEXT); 
      String userName = userContext.getUser(); 
      String workSpaceInUse = userContext.getCurrentWorkSpace();   
      FileItem item = null; 
      boolean fileNotExistFlag; 
      String fileExtension; 
      while (iterator.hasNext()) { 
       item = (FileItem) iterator.next(); 

       String content = item.getContentType(); 
       log.info(" content "+content); 
       log.info(" File Type Getting Uploaded :"+content); 
       if (!item.isFormField()) { 
      /* Here in IOUtils the Third Parameter true tells that the small chunks of data that comes need to be added to the same File */ 
      IOUtils.copy(fileItem.getInputStream(), new FileOutputStream(new File(threeDPath+"/"+year+"/"+series+"/"+uploadZipFileName),true));    
       } 
      } 
      return null; 
     } 
     } 
     catch(Exception e) { 
      return handleError(new RuntimeException("Unexpected error while uploading ZIP", e)); 
     } 
     return null; 
    }