Java Code Examples for org.apache.commons.fileupload.disk.DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD

The following are Jave code examples for showing how to use DEFAULT_SIZE_THRESHOLD of the org.apache.commons.fileupload.disk.DiskFileItemFactory class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: tkhoon   File: UploadHelper.java   View Source Code Vote up 5 votes
public static void init(ServletContext servletContext) {
    // 获取一个临时目录(使用 Tomcat 的 work 目录)
    File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
    // 创建 FileUpload 对象
    fileUpload = new ServletFileUpload(new DiskFileItemFactory(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD, repository));
    // 设置上传限制
    if (uploadLimit != 0) {
        fileUpload.setFileSizeMax(uploadLimit * 1024 * 1024); // 单位为 M
        if (logger.isDebugEnabled()) {
            logger.debug("[Smart] limit of uploading: {}M", uploadLimit);
        }
    }
}
 
Example 2
Project: Nicole   File: UploadHelper.java   View Source Code Vote up 5 votes
/**
 * 初始化
 */
public static void init(ServletContext servletContext) {
    File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
    servletFileUpload = new ServletFileUpload(new DiskFileItemFactory(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD, repository));
    int uploadLimit = ConfigHelper.getAppUploadLimit();
    if (uploadLimit != 0) {
        servletFileUpload.setFileSizeMax(uploadLimit * 1024 * 1024);
    }
}
 
Example 3
Project: testeverything   File: UploadHelper.java   View Source Code Vote up 5 votes
public static void init(ServletContext servletContext) {
    // 获取一个临时目录(使用 Tomcat 的 work 目录)
    File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
    // 创建 FileUpload 对象
    fileUpload = new ServletFileUpload(new DiskFileItemFactory(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD, repository));
    // 设置上传限制
    int uploadLimit = FrameworkConstant.UPLOAD_LIMIT;
    if (uploadLimit != 0) {
        fileUpload.setFileSizeMax(uploadLimit * 1024 * 1024);
    }
}
 
Example 4
Project: Cindy   File: HttpRequestServletImpl.java   View Source Code Vote up 5 votes
public HttpRequestServletImpl(HttpMethod httpMethod, HttpServletRequest request) throws IOException {
	this.request = request;
	this.httpMethod = httpMethod;

	if (ServletFileUpload.isMultipartContent(request)) {
		FileItemFactory fileItemFactory = new DiskFileItemFactory(
				DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD,
				new File(System.getProperty("java.io.tmpdir"))
		);
		ServletFileUpload fileUpload = new ServletFileUpload(fileItemFactory);
		try {
			this.bodyParameters = fileUpload.parseParameterMap(request);
		} catch (FileUploadException e) {
			throw new IOException(e);
		}
	} else {
		if (request.getMethod().equals("POST") || request.getMethod().equals("PUT")) {
			String contentType = request.getHeader("Content-Type");
			if (contentType != null && contentType.contains("application/x-www-form-urlencoded")) {
				this.bodyParameters = new HashMap<>();
				String[] parameters = IOUtils.readStreamAsString(request.getInputStream()).split("&");
				for (String parameter : parameters) {
					String[] keyVal = parameter.split("=");
					if (keyVal.length == 2) {
						String key = keyVal[0];
						String value = URLDecoder.decode(keyVal[1], "UTF-8");
						List<FileItem> items = new ArrayList<>();
						items.add(new FormFileItem(key, value));
						this.bodyParameters.put(key, items);
					}
				}
			} else {
				this.inputStream = request.getInputStream();
			}
		}
	}

	this.queryParameters = request.getParameterMap();
}
 
Example 5
Project: Lucee4   File: FormImpl.java   View Source Code Vote up 4 votes
private void initializeMultiPart(PageContext pc, boolean scriptProteced) {
   	// get temp directory
   	Resource tempDir = ((ConfigImpl)pc.getConfig()).getTempDirectory();
   	Resource tempFile;
   	
   	// Create a new file upload handler
   	final String encoding=getEncoding();
   	FileItemFactory factory = tempDir instanceof File? 
   			new DiskFileItemFactory(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD,(File)tempDir):
   				new DiskFileItemFactory();
   	
   	ServletFileUpload upload = new ServletFileUpload(factory);
   	upload.setHeaderEncoding(encoding);
   	//ServletRequestContext c = new ServletRequestContext(pc.getHttpServletRequest());
   	
   	
   	HttpServletRequest req = pc.getHttpServletRequest();
   	ServletRequestContext context = new ServletRequestContext(req) {
   		public String getCharacterEncoding() {
   			return encoding;
   		}
   	};
   	
   	// Parse the request
   	try {
   		FileItemIterator iter = upload.getItemIterator(context);
       	//byte[] value;
       	InputStream is;
       	ArrayList<URLItem> list=new ArrayList<URLItem>();
       	String fileName;
		while (iter.hasNext()) {
		    FileItemStream item = iter.next();

		    is=IOUtil.toBufferedInputStream(item.openStream());
		    if (item.getContentType()==null || StringUtil.isEmpty(item.getName())) {
		    	list.add(new URLItem(item.getFieldName(),new String(IOUtil.toBytes(is),encoding),false));	     
		    } 
		    else {
		    	fileName=getFileName();
		    	tempFile=tempDir.getRealResource(fileName);
		    	_fileItems.put(fileName,
		    			new Item(tempFile,item.getContentType(),item.getName(),item.getFieldName()));
				String value=tempFile.toString();
		    	IOUtil.copy(is, tempFile,true);
			    list.add(new URLItem(item.getFieldName(),value,false));	     
		    }       
		}
		
		
		raw= list.toArray(new URLItem[list.size()]);
		fillDecoded(raw,encoding,scriptProteced,pc.getApplicationContext().getSameFieldAsArray(SCOPE_FORM));
	} 
   	catch (Exception e) {
		
       	//throw new PageRuntimeException(Caster.toPageException(e));
       	fillDecodedEL(new URLItem[0],encoding,scriptProteced,pc.getApplicationContext().getSameFieldAsArray(SCOPE_FORM));
		initException=e;
	}
}
 
Example 6
Project: Lucee   File: FormImpl.java   View Source Code Vote up 4 votes
private void initializeMultiPart(PageContext pc, boolean scriptProteced) {
   	// get temp directory
   	Resource tempDir = ((ConfigImpl)pc.getConfig()).getTempDirectory();
   	Resource tempFile;
   	
   	// Create a new file upload handler
   	final String encoding=getEncoding();
   	FileItemFactory factory = tempDir instanceof File? 
   			new DiskFileItemFactory(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD,(File)tempDir):
   				new DiskFileItemFactory();
   	
   	ServletFileUpload upload = new ServletFileUpload(factory);
   	upload.setHeaderEncoding(encoding);
   	//ServletRequestContext c = new ServletRequestContext(pc.getHttpServletRequest());
   	
   	
   	HttpServletRequest req = pc.getHttpServletRequest();
   	ServletRequestContext context = new ServletRequestContext(req) {
   		@Override
		public String getCharacterEncoding() {
   			return encoding;
   		}
   	};
   	
   	// Parse the request
   	try {
   		FileItemIterator iter = upload.getItemIterator(context);
       	//byte[] value;
       	InputStream is;
       	ArrayList<URLItem> list=new ArrayList<URLItem>();
       	String fileName;
		while (iter.hasNext()) {
		    FileItemStream item = iter.next();

		    is=IOUtil.toBufferedInputStream(item.openStream());
		    if (item.getContentType()==null || StringUtil.isEmpty(item.getName())) {
		    	list.add(new URLItem(item.getFieldName(),new String(IOUtil.toBytes(is),encoding),false));	     
		    } 
		    else {
		    	fileName=getFileName();
		    	tempFile=tempDir.getRealResource(fileName);
		    	_fileItems.put(fileName,
		    			new Item(tempFile,item.getContentType(),item.getName(),item.getFieldName()));
				String value=tempFile.toString();
		    	IOUtil.copy(is, tempFile,true);
		    	
			    list.add(new URLItem(item.getFieldName(),value,false));	     
		    }       
		}
		
		raw= list.toArray(new URLItem[list.size()]);
		fillDecoded(raw,encoding,scriptProteced,pc.getApplicationContext().getSameFieldAsArray(SCOPE_FORM));
	} 
   	catch (Exception e) {
		
       	//throw new PageRuntimeException(Caster.toPageException(e));
       	fillDecodedEL(new URLItem[0],encoding,scriptProteced,pc.getApplicationContext().getSameFieldAsArray(SCOPE_FORM));
		initException=e;
	}
}
 
Example 7
Project: Kvantum   File: KvantumFileUpload.java   View Source Code Vote up 2 votes
/**
 * Initialize a new file upload handler that stores the uploaded
 * file in a temporary file, with the default {@link DiskFileItemFactory#DEFAULT_SIZE_THRESHOLD}
 * size threshold
 */
public KvantumFileUpload()
{
    this( DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD );
}