當前位置: 首頁>>代碼示例>>Java>>正文


Java FileItem.getSize方法代碼示例

本文整理匯總了Java中org.apache.commons.fileupload.FileItem.getSize方法的典型用法代碼示例。如果您正苦於以下問題:Java FileItem.getSize方法的具體用法?Java FileItem.getSize怎麽用?Java FileItem.getSize使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.commons.fileupload.FileItem的用法示例。


在下文中一共展示了FileItem.getSize方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getHashtable

import org.apache.commons.fileupload.FileItem; //導入方法依賴的package包/類
public Hashtable getHashtable(HttpServletRequest request)
        throws FileUploadException, IOException {
    Hashtable ht = new Hashtable();
    DiskFileUpload upload = new DiskFileUpload();
    List fileItems = upload.parseRequest(request);
    Iterator iter = fileItems.iterator();
    while (iter.hasNext()) {
        FileItem item = (FileItem) iter.next();
        if (item.isFormField()) {
            ht.put(item.getFieldName(), item.getString());
        } else {
            if (item.getName().equals("")) {
                //ht.put(item.getFieldName(), null);
            } else if (item.getSize() == 0) {
                //ht.put(item.getFieldName(), null);
            } else {
                ht.put(item.getFieldName(), item.getInputStream());
            }
        }
    }
    return ht;
}
 
開發者ID:cecid,項目名稱:hermes,代碼行數:23,代碼來源:PartnershipPageletAdaptor.java

示例2: getHashtable

import org.apache.commons.fileupload.FileItem; //導入方法依賴的package包/類
public Hashtable getHashtable(HttpServletRequest request)
    throws FileUploadException, IOException {
	Hashtable ht = new Hashtable();
	DiskFileUpload upload = new DiskFileUpload();
	List fileItems = upload.parseRequest(request);
	Iterator iter = fileItems.iterator();
	while (iter.hasNext()) {
	    FileItem item = (FileItem) iter.next();
	    if (item.isFormField()) {
	        ht.put(item.getFieldName(), item.getString());
	    } else {
	        if (item.getName().equals("")) {
	            //ht.put(item.getFieldName(), null);
	        } else if (item.getSize() == 0) {
	            //ht.put(item.getFieldName(), null);
	        } else {
	            ht.put(item.getFieldName(), item.getInputStream());
	        }
	    }
	}
	return ht;
}
 
開發者ID:cecid,項目名稱:hermes,代碼行數:23,代碼來源:PartnershipPageletAdaptor.java

示例3: processFileField

import org.apache.commons.fileupload.FileItem; //導入方法依賴的package包/類
/**
 * Process multipart request item as file field. The name and FileItem
 * object of each file field will be added as attribute of the given
 * HttpServletRequest. If a FileUploadException has occurred when the file
 * size has exceeded the maximum file size, then the FileUploadException
 * will be added as attribute value instead of the FileItem object.
 *
 * @param fileField The file field to be processed.
 * @param request   The involved HttpServletRequest.
 */
private void processFileField(FileItem fileField, HttpServletRequest request) {
    if (fileField.getName().length() <= 0) {
        // No file uploaded.
        request.setAttribute(fileField.getFieldName(), null);
    } else if (maxFileSize > 0 && fileField.getSize() > maxFileSize) {
        // File size exceeds maximum file size.
        request.setAttribute(fileField.getFieldName(), new FileUploadException("File size " +
                "exceeds maximum file size of " + maxFileSize + " bytes."));
        // Immediately delete temporary file to free up memory and/or disk
        // space.
        fileField.delete();
    } else {
        // File uploaded with good size.
        request.setAttribute(fileField.getFieldName(), fileField);
    }
}
 
開發者ID:helicalinsight,項目名稱:helicalinsight,代碼行數:27,代碼來源:MultipartFilter.java

示例4: checkAttachmentSize

import org.apache.commons.fileupload.FileItem; //導入方法依賴的package包/類
/**
 * Check attachment size and throw {@link MaxLengthReachedException}
 *
 * @param file
 *            the file to upload
 * @throws MaxLengthReachedException
 *             exception for file is to large
 */
public static void checkAttachmentSize(FileItem file) throws MaxLengthReachedException {
    long maxAttachmentSize = 0;

    if (file.getContentType().startsWith(CONTENT_TYPE_IMAGE_PREFIX)) {
        maxAttachmentSize = Long.parseLong(CommunoteRuntime.getInstance()
                .getConfigurationManager().getApplicationConfigurationProperties()
                .getProperty(ApplicationProperty.IMAGE_MAX_UPLOAD_SIZE));
        if (!(maxAttachmentSize >= file.getSize())) {
            throw new MaxLengthReachedException();
        }
    }

    // check that attachment size is enough
    maxAttachmentSize = Long.parseLong(CommunoteRuntime.getInstance().getConfigurationManager()
            .getApplicationConfigurationProperties()
            .getProperty(ApplicationProperty.ATTACHMENT_MAX_UPLOAD_SIZE));

    if (!(maxAttachmentSize >= file.getSize())) {
        throw new MaxLengthReachedException();
    }
}
 
開發者ID:Communote,項目名稱:communote-server,代碼行數:30,代碼來源:AttachmentResourceHelper.java

示例5: makeTempFromFileItem

import org.apache.commons.fileupload.FileItem; //導入方法依賴的package包/類
/**
	 * {@inheritDoc}
	 * <pre>
	 * BLOBに保存するためヘッダ情報 + ファイル本體の形式の一時ファイルを作成します。
	 * </pre>
	 */
	@Override
	protected File makeTempFromFileItem(final FileItem fileItem) throws Exception {
//		log.error("makeTempFromFileItem", new Exception());
		String fileName = FileUtil.getFileName(fileItem.getName());
		long length = fileItem.getSize();
		File file = null;
		InputStream is = fileItem.getInputStream();
		try {
			file = this.makeBlobTempFile(fileName, length, is);
		} finally {
			is.close();
		}
//		this.tempFile = file;
		return file;
	}
 
開發者ID:takayanagi2087,項目名稱:dataforms,代碼行數:22,代碼來源:BlobFileStore.java

示例6: handlePost

import org.apache.commons.fileupload.FileItem; //導入方法依賴的package包/類
@RequestMapping(method = RequestMethod.POST)
protected String handlePost(RedirectAttributes redirectAttributes,
                                       HttpServletRequest request
                                       ) throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();

    try {
        if (ServletFileUpload.isMultipartContent(request)) {

            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            List<?> items = upload.parseRequest(request);
            for (Object o : items) {
                FileItem item = (FileItem) o;

                if ("file".equals(item.getFieldName()) && !StringUtils.isBlank(item.getName())) {
                    if (item.getSize() > MAX_PLAYLIST_SIZE_MB * 1024L * 1024L) {
                        throw new Exception("The playlist file is too large. Max file size is " + MAX_PLAYLIST_SIZE_MB + " MB.");
                    }
                    String playlistName = FilenameUtils.getBaseName(item.getName());
                    String fileName = FilenameUtils.getName(item.getName());
                    String format = StringUtils.lowerCase(FilenameUtils.getExtension(item.getName()));
                    String username = securityService.getCurrentUsername(request);
                    Playlist playlist = playlistService.importPlaylist(username, playlistName, fileName,
                                                                       item.getInputStream(), null);
                    map.put("playlist", playlist);
                }
            }
        }
    } catch (Exception e) {
        map.put("error", e.getMessage());
    }

    redirectAttributes.addFlashAttribute("model", map);
    return "redirect:importPlaylist";
}
 
開發者ID:airsonic,項目名稱:airsonic,代碼行數:37,代碼來源:ImportPlaylistController.java

示例7: HttpUploadReader

import org.apache.commons.fileupload.FileItem; //導入方法依賴的package包/類
HttpUploadReader(FileItem item) throws IOException {
    this.item = item;
    fieldName = item.getFieldName();
    fileName = item.getName();
    contentType = item.getContentType();
    size = item.getSize();
    inputStream = item.getInputStream();
}
 
開發者ID:heisedebaise,項目名稱:tephra,代碼行數:9,代碼來源:HttpUploadReader.java

示例8: handleRequestInternal

import org.apache.commons.fileupload.FileItem; //導入方法依賴的package包/類
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();

    try {
        if (ServletFileUpload.isMultipartContent(request)) {

            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            List<?> items = upload.parseRequest(request);
            for (Object o : items) {
                FileItem item = (FileItem) o;

                if ("file".equals(item.getFieldName()) && !StringUtils.isBlank(item.getName())) {
                    if (item.getSize() > MAX_PLAYLIST_SIZE_MB * 1024L * 1024L) {
                        throw new Exception("The playlist file is too large. Max file size is " + MAX_PLAYLIST_SIZE_MB + " MB.");
                    }
                    String playlistName = FilenameUtils.getBaseName(item.getName());
                    String fileName = FilenameUtils.getName(item.getName());
                    String format = StringUtils.lowerCase(FilenameUtils.getExtension(item.getName()));
                    String username = securityService.getCurrentUsername(request);
                    Playlist playlist = playlistService.importPlaylist(username, playlistName, fileName, format, item.getInputStream(), null);
                    map.put("playlist", playlist);
                }
            }
        }
    } catch (Exception e) {
        map.put("error", e.getMessage());
    }

    ModelAndView result = super.handleRequestInternal(request, response);
    result.addObject("model", map);
    return result;
}
 
開發者ID:sindremehus,項目名稱:subsonic,代碼行數:35,代碼來源:ImportPlaylistController.java

示例9: createParam

import org.apache.commons.fileupload.FileItem; //導入方法依賴的package包/類
/**
 * 創建請求對象
 */
public static Param createParam(HttpServletRequest request) throws IOException {
    List<FormParam> formParamList = new ArrayList<FormParam>();
    List<FileParam> fileParamList = new ArrayList<FileParam>();
    try{
        Map<String, List<FileItem>> fileItemListMap = servletFileUpload.parseParameterMap(request);
        if (CollectionUtil.isNotEmpty(fileItemListMap)) {
            for (Map.Entry<String, List<FileItem>> fileItemListEntry : fileItemListMap.entrySet()) {
                String fieldName = fileItemListEntry.getKey();
                List<FileItem> fileItemList = fileItemListEntry.getValue();
                if (CollectionUtil.isNotEmpty(fileItemList)) {
                    for (FileItem fileItem : fileItemList) {
                        if(fileItem.isFormField()){
                            String fieldValue = fileItem.getString("UTF-8");
                            formParamList.add(new FormParam(fieldName, fieldValue));
                        } else{
                            String fileName = FileUtil.getRealFileName(new String(fileItem.getName().getBytes(), "UTF-8"));
                            if (StringUtil.isNotEmpty(fileName)) {
                                long fileSize = fileItem.getSize();
                                String contentType = fileItem.getContentType();
                                InputStream inputSteam = fileItem.getInputStream();
                                fileParamList.add(new FileParam(fieldName, fileName, fileSize, contentType, inputSteam));
                            }
                        }
                    }
                }
            }
        }
    }catch (FileUploadException e){
        LOGGER.error("create param failure", e);
        throw new RuntimeException(e);
    }
    return new Param(formParamList, fileParamList);
}
 
開發者ID:edagarli,項目名稱:Nicole,代碼行數:37,代碼來源:UploadHelper.java

示例10: createMultipartParamList

import org.apache.commons.fileupload.FileItem; //導入方法依賴的package包/類
public static List<Object> createMultipartParamList(HttpServletRequest request) throws Exception {
    // 定義參數列表
    List<Object> paramList = new ArrayList<Object>();
    // 創建兩個對象,分別對應 普通字段 與 文件字段
    Map<String, String> fieldMap = new HashMap<String, String>();
    List<Multipart> multipartList = new ArrayList<Multipart>();
    // 獲取並遍曆表單項
    List<FileItem> fileItemList;
    try {
        fileItemList = fileUpload.parseRequest(request);
    } catch (FileUploadBase.FileSizeLimitExceededException e) {
        // 異常轉換(拋出自定義異常)
        throw new UploadException(e);
    }
    for (FileItem fileItem : fileItemList) {
        // 分兩種情況處理表單項
        String fieldName = fileItem.getFieldName();
        if (fileItem.isFormField()) {
            // 處理普通字段
            String fieldValue = fileItem.getString(FrameworkConstant.DEFAULT_CHARSET);
            fieldMap.put(fieldName, fieldValue);
        } else {
            // 處理文件字段
            String originalFileName = FileUtil.getRealFileName(fileItem.getName());
            String uploadedFileName = FileUtil.getEncodedFileName(originalFileName);
            String contentType = fileItem.getContentType();
            long fileSize = fileItem.getSize();
            InputStream inputSteam = fileItem.getInputStream();
            // 創建 Multipart 對象,並將其添加到 multipartList 中
            Multipart multipart = new Multipart(uploadedFileName, contentType, fileSize, inputSteam);
            multipartList.add(multipart);
            // 將所上傳文件的文件名存入 fieldMap 中
            fieldMap.put(fieldName, uploadedFileName);
        }
    }
    // 初始化參數列表
    paramList.add(fieldMap);
    if (multipartList.size() > 1) {
        paramList.add(multipartList);
    } else if (multipartList.size() == 1) {
        paramList.add(multipartList.get(0));
    } else {
        paramList.add(null);
    }
    // 返回參數列表
    return paramList;
}
 
開發者ID:thinkhoon,項目名稱:tkhoon,代碼行數:48,代碼來源:UploadHelper.java

示例11: getFileParameterValue

import org.apache.commons.fileupload.FileItem; //導入方法依賴的package包/類
/**
 * Responsible for constructing a FileBean object for the named file parameter. If there is no
 * file parameter with the specified name this method should return null.
 *
 * @param name the name of the file parameter
 * @return a FileBean object wrapping the uploaded file
 */
public FileBean getFileParameterValue(String name) {
    final FileItem item = this.files.get(name);
    if (item == null
            || ((item.getName() == null || item.getName().length() == 0) && item.getSize() == 0)) {
        return null;
    }
    else {
        // Attempt to ensure the file name is just the basename with no path included
        String filename = item.getName();
        int index;
        if (WINDOWS_PATH_PREFIX_PATTERN.matcher(filename).find())
            index = filename.lastIndexOf('\\');
        else
            index = filename.lastIndexOf('/');
        if (index >= 0 && index + 1 < filename.length() - 1)
            filename = filename.substring(index + 1);

        // Use an anonymous inner subclass of FileBean that overrides all the
        // methods that rely on having a File present, to use the FileItem
        // created by commons upload instead.
        return new FileBean(null, item.getContentType(), filename, this.charset) {
            @Override public long getSize() { return item.getSize(); }

            @Override public InputStream getInputStream() throws IOException {
                return item.getInputStream();
            }

            @Override public void save(File toFile) throws IOException {
                try {
                    item.write(toFile);
                    delete();
                }
                catch (Exception e) {
                    if (e instanceof IOException) throw (IOException) e;
                    else {
                        IOException ioe = new IOException("Problem saving uploaded file.");
                        ioe.initCause(e);
                        throw ioe;
                    }
                }
            }

            @Override
            public void delete() throws IOException { item.delete(); }
        };
    }
}
 
開發者ID:nkasvosve,項目名稱:beyondj,代碼行數:55,代碼來源:CommonsMultipartWrapper.java

示例12: getCenterSource

import org.apache.commons.fileupload.FileItem; //導入方法依賴的package包/類
protected Source getCenterSource(HttpServletRequest request) {

        PropertyTree dom = new PropertyTree();
        dom.setProperty("/partnership", "");

        boolean isMultipart = FileUpload.isMultipartContent(request);

        if (isMultipart) {
            DiskFileUpload upload = new DiskFileUpload();
            try {
                FileItem realFileItem = null;
                boolean hasFileField = false;
                List fileItems = upload.parseRequest(request);

                Iterator iter = fileItems.iterator();
                while (iter.hasNext()) {
                    FileItem item = (FileItem) iter.next();

                    if (item.isFormField()) {
                        if (item.getFieldName().equals("party_name")) {
                            selectedPartyName = item.getString();
                        }
                    } else {
                        hasFileField = true;

                        if (item.getName().equals("")) {
                            request.setAttribute(ATTR_MESSAGE,
                                    "No file specified");
                        } else if (item.getSize() == 0) {
                            request.setAttribute(ATTR_MESSAGE,
                                    "The file is no content");
                        } else if (!item.getContentType().equalsIgnoreCase(
                                "text/xml")) {
                            request.setAttribute(ATTR_MESSAGE,
                                    "It is not a xml file");
                        } else {
                            realFileItem = item;
                        }
                    }
                }

                if (!hasFileField) {
                    request.setAttribute(ATTR_MESSAGE,
                            "There is no file field in the request paramters");
                }

                if (selectedPartyName.equalsIgnoreCase("")) {
                    request
                            .setAttribute(ATTR_MESSAGE,
                                    "There is no party name field in the request paramters");
                } else {
                    X_ATTR_PARTY_NAME = "[@" + X_TP_NAMESPACE + "partyName='"
                            + selectedPartyName + "']";
                }

                if (realFileItem != null
                        && !selectedPartyName.equalsIgnoreCase("")) {
                    String errorMessage = processUploadedXml(dom, realFileItem);
                    if (errorMessage != null) {
                        request.setAttribute(ATTR_MESSAGE, errorMessage);
                    }

                }
            } catch (Exception e) {
                EbmsProcessor.core.log.error(
                        "Exception throw when upload the file", e);
                request.setAttribute(ATTR_MESSAGE,
                        "Exception throw when upload the file");
            }
        }

        return dom.getSource();
    }
 
開發者ID:cecid,項目名稱:hermes,代碼行數:74,代碼來源:AgreementUploadPageletAdaptor.java


注:本文中的org.apache.commons.fileupload.FileItem.getSize方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。