List of usage examples for org.apache.commons.fileupload FileItem getFieldName
String getFieldName();
From source file:com.xpn.xwiki.web.Utils.java
/** * Get the name of an uploaded file, corresponding to the specified form field. * /*w w w.j ava 2 s . c o m*/ * @param filelist the list of uploaded files, computed by the FileUpload plugin * @param name the name of the form field * @return the original name of the file, if the specified field name does correspond to an uploaded file, or * {@code null} otherwise */ public static String getFileName(List<FileItem> filelist, String name) { for (FileItem item : filelist) { if (name.equals(item.getFieldName())) { return item.getName(); } } return null; }
From source file:com.xpn.xwiki.web.Utils.java
/** * Get the content of an uploaded file, corresponding to the specified form field. * /*ww w .j a va 2 s.co m*/ * @param filelist the list of uploaded files, computed by the FileUpload plugin * @param name the name of the form field * @return the content of the file, if the specified field name does correspond to an uploaded file, or {@code null} * otherwise * @throws XWikiException if the file cannot be read due to an underlying I/O exception */ public static byte[] getContent(List<FileItem> filelist, String name) throws XWikiException { for (FileItem item : filelist) { if (name.equals(item.getFieldName())) { byte[] data = new byte[(int) item.getSize()]; InputStream fileis = null; try { fileis = item.getInputStream(); fileis.read(data); } catch (IOException e) { throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_UPLOAD_FILE_EXCEPTION, "Exception while reading uploaded parsed file", e); } finally { IOUtils.closeQuietly(fileis); } return data; } } return null; }
From source file:com.zlfun.framework.excel.ExcelUtils.java
public static <T> List<T> httpInput(HttpServletRequest request, Class<T> clazz) { List<T> result = new ArrayList<T>(); //?? /*ww w. ja v a 2s . c o m*/ DiskFileItemFactory factory = new DiskFileItemFactory(); //?? String path = request.getSession().getServletContext().getRealPath("/"); factory.setRepository(new File(path)); // ?? factory.setSizeThreshold(1024 * 1024); ServletFileUpload upload = new ServletFileUpload(factory); try { //? List<FileItem> list = (List<FileItem>) upload.parseRequest(request); for (FileItem item : list) { //???? String name = item.getFieldName(); //? ?? ? if (item.isFormField()) { //? ?????? String value = item.getString(); request.setAttribute(name, value); } //? ?? else { /** * ?? ?? */ //??? String value = item.getName(); //???? fill(clazz, result, value, item.getInputStream()); //?? } } } catch (FileUploadException ex) { // TODO Auto-generated catch block excel=null; Logger.getLogger(ExcelUtils.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(ExcelUtils.class.getName()).log(Level.SEVERE, null, ex); } return result; }
From source file:com.xpn.xwiki.web.Utils.java
/** * Process a multi-part request, extracting all the uploaded files. * //w ww . ja v a 2 s . c om * @param request the current request to process * @param context the current context * @return the instance of the {@link FileUploadPlugin} used to parse the uploaded files */ public static FileUploadPlugin handleMultipart(HttpServletRequest request, XWikiContext context) { FileUploadPlugin fileupload = null; try { if (request instanceof MultipartRequestWrapper) { fileupload = new FileUploadPlugin("fileupload", "fileupload", context); fileupload.loadFileList(context); context.put("fileuploadplugin", fileupload); MultipartRequestWrapper mpreq = (MultipartRequestWrapper) request; List<FileItem> fileItems = fileupload.getFileItems(context); for (FileItem item : fileItems) { if (item.isFormField()) { String sName = item.getFieldName(); String sValue = item.getString(context.getWiki().getEncoding()); mpreq.setParameter(sName, sValue); } } } } catch (Exception e) { if ((e instanceof XWikiException) && (((XWikiException) e).getCode() == XWikiException.ERROR_XWIKI_APP_FILE_EXCEPTION_MAXSIZE)) { context.put("exception", e); } else { e.printStackTrace(); } } return fileupload; }
From source file:com.dien.upload.server.UploadServlet.java
/** * Utility method to get a fileItem from a vector using the attribute name. * //w w w . j a va 2 s . c o m * @param sessionFiles * @param attrName * @return fileItem found or null */ public static FileItem findItemByFieldName(List<FileItem> sessionFiles, String attrName) { if (sessionFiles != null) { for (FileItem fileItem : sessionFiles) { if (fileItem.getFieldName().equalsIgnoreCase(attrName)) { return fileItem; } } } return null; }
From source file:fr.paris.lutece.util.http.MultipartUtil.java
/** * Convert a HTTP request to a {@link MultipartHttpServletRequest} * @param nSizeThreshold the size threshold * @param nRequestSizeMax the request size max * @param bActivateNormalizeFileName true if the file name must be normalized, false otherwise * @param request the HTTP request/*from w ww . ja v a 2 s .c o m*/ * @return a {@link MultipartHttpServletRequest}, null if the request does not have a multipart content * @throws SizeLimitExceededException exception if the file size is too big * @throws FileUploadException exception if an unknown error has occurred */ public static MultipartHttpServletRequest convert(int nSizeThreshold, long nRequestSizeMax, boolean bActivateNormalizeFileName, HttpServletRequest request) throws SizeLimitExceededException, FileUploadException { if (isMultipart(request)) { // Create a factory for disk-based file items DiskFileItemFactory factory = new DiskFileItemFactory(); // Set factory constraints factory.setSizeThreshold(nSizeThreshold); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Set overall request size constraint upload.setSizeMax(nRequestSizeMax); // get encoding to be used String strEncoding = request.getCharacterEncoding(); if (strEncoding == null) { strEncoding = EncodingService.getEncoding(); } Map<String, FileItem> mapFiles = new HashMap<String, FileItem>(); Map<String, String[]> mapParameters = new HashMap<String, String[]>(); List<FileItem> listItems = upload.parseRequest(request); // Process the uploaded items for (FileItem item : listItems) { if (item.isFormField()) { String strValue = StringUtils.EMPTY; try { if (item.getSize() > 0) { strValue = item.getString(strEncoding); } } catch (UnsupportedEncodingException ex) { if (item.getSize() > 0) { // if encoding problem, try with system encoding strValue = item.getString(); } } // check if item of same name already in map String[] curParam = mapParameters.get(item.getFieldName()); if (curParam == null) { // simple form field mapParameters.put(item.getFieldName(), new String[] { strValue }); } else { // array of simple form fields String[] newArray = new String[curParam.length + 1]; System.arraycopy(curParam, 0, newArray, 0, curParam.length); newArray[curParam.length] = strValue; mapParameters.put(item.getFieldName(), newArray); } } else { // multipart file field, if the parameter filter ActivateNormalizeFileName is set to true //all file name will be normalize mapFiles.put(item.getFieldName(), bActivateNormalizeFileName ? new NormalizeFileItem(item) : item); } } return new MultipartHttpServletRequest(request, mapFiles, mapParameters); } return null; }
From source file:com.acciente.commons.htmlform.Parser.java
private static Map parsePOSTMultiPart(HttpServletRequest oRequest, int iStoreFileOnDiskThresholdInBytes, File oUploadedFileStorageDir) throws FileUploadException, IOException, ParserException { Map oMultipartParams = new HashMap(); // we have multi-part content, we process it with apache-commons-fileupload ServletFileUpload oMultipartParser = new ServletFileUpload( new DiskFileItemFactory(iStoreFileOnDiskThresholdInBytes, oUploadedFileStorageDir)); List oFileItemList = oMultipartParser.parseRequest(oRequest); for (Iterator oIter = oFileItemList.iterator(); oIter.hasNext();) { FileItem oFileItem = (FileItem) oIter.next(); // we support the variable name to use the full syntax allowed in non-multipart forms // so we use parseParameterSpec() to support array and map variable syntaxes in multi-part mode Reader oParamNameReader = null; ParameterSpec oParameterSpec = null; try {// w w w.ja va 2 s. c o m oParamNameReader = new StringReader(oFileItem.getFieldName()); oParameterSpec = Parser.parseParameterSpec(oParamNameReader, oFileItem.isFormField() ? Symbols.TOKEN_VARTYPE_STRING : Symbols.TOKEN_VARTYPE_FILE); } finally { if (oParamNameReader != null) { oParamNameReader.close(); } } if (oFileItem.isFormField()) { Parser.addParameter2Form(oMultipartParams, oParameterSpec, oFileItem.getString(), false); } else { Parser.addParameter2Form(oMultipartParams, oParameterSpec, oFileItem); } } return oMultipartParams; }
From source file:com.globalsight.everest.webapp.pagehandler.administration.vendors.VendorHelper.java
/** * Save the request parameters from the CV/Resume page *//*from w ww .jav a 2 s.c o m*/ public static void saveCV(Vendor vendor, HttpServletRequest request) throws EnvoyServletException { // Create a new file upload handler DiskFileUpload upload = new DiskFileUpload(); String radioValue = null; String resumeText = null; boolean doUpload = false; byte[] data = null; String filename = null; // Parse the request try { List /* FileItem */ items = upload.parseRequest(request); // Process the uploaded items Iterator iter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); if (item.isFormField()) { String name = item.getFieldName(); String value = EditUtil.utf8ToUnicode(item.getString()); if (name.equals("radioBtn")) { radioValue = value; } else if (name.equals("resumeText")) { resumeText = value; } } else { filename = item.getName(); if (filename == null || filename.equals("")) { // user hit done button but didn't modify the page continue; } else { doUpload = true; data = item.get(); } } } if (radioValue != null) { if (radioValue.equals("doc") && doUpload) { vendor.setResume(filename, data); try { ServerProxy.getVendorManagement().saveResumeFile(vendor); } catch (Exception e) { throw new EnvoyServletException(e); } } else if (radioValue.equals("text")) { vendor.setResume(resumeText); } } } catch (FileUploadException fe) { throw new EnvoyServletException(fe); } }
From source file:com.intel.cosbench.controller.handler.ConfigHandler.java
@SuppressWarnings("unchecked") private InputStream retrieveConfigStream(HttpServletRequest request) throws Exception { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); for (FileItem item : (List<FileItem>) upload.parseRequest(request)) if (item.getFieldName().equals("config")) return item.getInputStream(); throw new BadRequestException(); }
From source file:de.micromata.genome.gwiki.page.impl.actionbean.CommonMultipartRequest.java
public void addFileItem(FileItem item) { fileItems.put(item.getFieldName(), item); }