簡體   English   中英

如何使用 Apache 文件上傳工具根據文件名設置最大文件大小

[英]How to set maximum file size based on file name using Apache File upload utils

我有一個要求,我需要為不同的情況允許不同的最大文件大小。 示例:允許 5 MB 用於簡歷,僅 3 MB 用於成績單。

我正在使用以下代碼使用 apache 文件上傳工具上傳文件。

        ServletFileUpload upload = new ServletFileUpload();
        upload.setSizeMax(500000000);
        upload.setProgressListener(aupl);
        FileItemIterator  iter = upload.getItemIterator(req);           

        while (iter.hasNext()) {
            FileItemStream item = iter.next();
            if (!item.isFormField()) {                  
                form_name = item.getFieldName();        
        InputStream stream = item.openStream();     
        FileOutputStream fop = new FileOutputStream(new File(temp_location));
        Streams.copy(stream, fop, true);                
            }             
        }                

我可以找到字段名稱的唯一方法是使用 item.getFieldName(),而且我只能在執行 upload.getItemIterator 之后執行此操作,但必須在上傳時設置 setSizeMax(500..),然后再調用 upload.getItemIterator。

有解決這個問題的方法嗎? 如果沒有解決方案,您能否建議任何其他處理此問題的文件上傳 API。

謝謝

如果您改為循環通過 FileItem object 而不是 FileItemStream 對象,您需要做的就是設置一些恆定的最大大小值並將每個項目與適當的值進行比較。 如果項目超出大小,請適當處理(拋出新異常,丟棄文件,無論您想做什么),否則繼續正常運行。

final long MAX_RESUME_SIZE = 5242880; // 5MB -> 5 * 1024 * 1024
final long MAX_TRANS_SIZE = 3145728; // 3MB -> 3 * 1024 * 1024

DiskFileItemFactory factory = new DiskFileItemFactory();
String fileDir = "your write-to location";
File dest = new File(fileDir);
if(!dest.isDirectory()){ dest.mkdir(); }
factory.setRepository(dest);
ServletFileUpload upload = new ServletFileUpload(factory);

for (FileItem item: upload.parseRequest(request)) { // request -> the HttpServletRequest
    if(!item.isFormField(){
        if(evaluateSize(item)){
            // handle as normal
        }else{
            // handle as too large
        }
    }
} // end while

private boolean evaluateSize(FileItem item){
    if(/* type is Resume */ && item.getSize() <= MAX_RESUME_SIZE){
        return true;
    }else if(/* type is Transcript */ && item.getSize() <= MAX_TRANS_SIZE){
        return true;
    }

    // assume too large
    return false;
}

當然,如果有兩種以上的文件類型,您將不得不添加更多邏輯,但您可以看到在編寫之前比較文件大小非常簡單。

假設非形式變量的數量是有限的(並且您可以強制執行),只需使用迭代器並使用 stream 的包裝來在字節總數時拋出異常(存在大量基本計數器的實現 - 見 commons- io 例如)超過 N,其中 N 在構造函數中作為限制提供。

eg

    long limit = 500000; // bytes
    long cumulativeSize=0;

while { 
    if (limit - cumulativeSize <=0) break; 
...
...
... // FileItem
    InputStream stream = item.openStream();
    stream = new LimiterStream(stream,100000);
    Streams.copy(stream,fop,true);
    FileOutputStream fop = new FileOutputStream(new File(temp_location));
    cumulativeSize += stream.getCount(); // you'd implement this too, to keep a running count
    catch (SizeExceededException e  )  {
            System.out.println("you exceeded the limit I set of "+e.getLimit(); // implemented 
            break;
     }
    ...
} // end while

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM