Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用Apache file upload utils根据文件名设置最大文件大小_Java_File Upload_Apache Commons - Fatal编程技术网

Java 如何使用Apache file upload utils根据文件名设置最大文件大小

Java 如何使用Apache file upload utils根据文件名设置最大文件大小,java,file-upload,apache-commons,Java,File Upload,Apache Commons,我有一个要求,我需要允许不同的最大文件大小不同的情况。示例:简历允许5MB,成绩单允许3MB 我使用以下代码使用apache file upload utils上传文件 ServletFileUpload upload = new ServletFileUpload(); upload.setSizeMax(500000000); upload.setProgressListener(aupl); FileItemIterator

我有一个要求,我需要允许不同的最大文件大小不同的情况。示例:简历允许5MB,成绩单允许3MB

我使用以下代码使用apache file upload utils上传文件

        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之后才能找到,但在调用upload.getItemIterator之前,必须在upload时设置setSizeMax(500..)

这个问题有解决办法吗?如果没有解决方案,您能否建议其他处理此问题的文件上载API


感谢

假设非形式变量的数量是有限的(您可以强制执行),只要使用迭代器并在流周围使用包装,当总字节数(存在大量基本计数器的实现-例如,请参阅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
eg
长限=500000;//字节
长累积尺寸=0;
而{

if(limit-cumulativeSize如果您改为通过FileItem对象而不是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;
}
final long MAX\u RESUME\u SIZE=5242880;//5MB->5*1024*1024
最终最大传输长度=3145728;//3MB->3*1024*1024
DiskFileItemFactory=新的DiskFileItemFactory();
String fileDir=“您的写入位置”;
File dest=新文件(fileDir);
如果(!dest.isDirectory()){dest.mkdir();}
工厂设置存储库(dest);
ServletFileUpload upload=新的ServletFileUpload(工厂);
对于(FileItem项:upload.parseRequest(request)){//request->HttpServletRequest
如果(!item.isFormField(){
if(评估(项目)){
//照常处理
}否则{
//把手太大
}
}
}//结束时
私有布尔值evaluateSize(FileItem){

如果(/*类型为Resume*/&&item.getSize()也许您可以为每种文件类型使用不同的servlet。500000000也是500MB—有点大?出于测试目的,请将maxFileSize设置为大于0,因为我很难对其进行测试,假设并将其设置为0,当我将其设置为1时,它起作用了。您还可以计算formfields的长度,并将其添加到cumulativeSize中。。。。