Java 类org.apache.commons.fileupload.DiskFileUpload 实例源码

项目:hermes    文件:PartnershipPageletAdaptor.java   
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;
}
项目:hermes    文件:PartnershipPageletAdaptor.java   
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;
}
项目:hermes    文件:PartnershipPageletAdaptor.java   
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;
}
项目:hermes    文件:PartnershipPageletAdaptor.java   
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;
}
项目:Deskera-HRMS    文件:fileUploader.java   
public static void parseRequest(HttpServletRequest request, HashMap<String, String> arrParam, ArrayList<FileItem> fi, boolean fileUpload) throws ServiceException {
    DiskFileUpload fu = new DiskFileUpload();
    FileItem fi1 = null;
    List fileItems = null;
    try {
        fileItems = fu.parseRequest(request);
    } catch (FileUploadException e) {
        throw ServiceException.FAILURE("Admin.createUser", e);
    }
    for (Iterator k = fileItems.iterator(); k.hasNext();) {
        fi1 = (FileItem) k.next();
        if (fi1.isFormField()) {
            arrParam.put(fi1.getFieldName(), fi1.getString());
        } else {
            try {
                String fileName = new String(fi1.getName().getBytes(), "UTF8");
                if (fi1.getSize() != 0) {
                    fi.add(fi1);
                    fileUpload = true;
                }
            } catch (UnsupportedEncodingException ex) {
            }
        }
    }
}
项目:tern    文件:MultiPartEnabledRequest.java   
private List uploadFiles(HttpServletRequest req) throws FileUploadException 
{
  DiskFileUpload upload = new DiskFileUpload();

  /*try 
  {
    upload.setSizeThreshold(res.getInteger("file.upload.size.threshold"));
  } 
  catch (MissingResourceException e)
  {
    // use defaults
  }*/

  try 
  {
    upload.setSizeMax(MaxFileSize);
  } 
  catch (MissingResourceException e) 
  {
    // use defaults
  }

  /*try 
  {
    upload.setRepositoryPath(res.getString("file.upload.repository"));
  } 
  catch (MissingResourceException e) 
  {
    // use defaults
  }*/

  List all = new DiskFileUpload().parseRequest(req);
  return all;
}
项目:Deskera-HRMS    文件:fileUploader.java   
public static void parseRequest(HttpServletRequest request, HashMap<String, String> arrParam, ArrayList<FileItem> fi, boolean fileUpload,HashMap<Integer,String> filemap) throws ServiceException {
    DiskFileUpload fu = new DiskFileUpload();
    FileItem fi1 = null;
    List fileItems = null;
    int i=0;
    try {
        fileItems = fu.parseRequest(request);
    } catch (FileUploadException e) {
        throw ServiceException.FAILURE("Admin.createUser", e);
    }
    for (Iterator k = fileItems.iterator(); k.hasNext();) {
        fi1 = (FileItem) k.next();
        try {
            if (fi1.isFormField()) {
                arrParam.put(fi1.getFieldName(), fi1.getString("UTF-8"));
            } else {

                String fileName = new String(fi1.getName().getBytes(), "UTF8");
                if (fi1.getSize() != 0) {
                    fi.add(fi1);
                    filemap.put(i,fi1.getFieldName());
                    i++;
                    fileUpload = true;
                }
            }
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }
    }
}
项目:lams    文件:UploadFileUtil.java   
/**
    * Given the current servlet request and the name of a temporary directory, get a list of all the uploaded items
    * from a multipart form. This includes any uploaded files and the "normal" input fields on the form.
    *
    * @param request
    *            - current servlet request
    * @param useLargeFileSize
    *            - use the large file size. If not true, use the standard file size.
    * @param tempDirName
    *            - the name of the directory into which temporary files can be written
    * @return List of items, of type FileItem
    */
   @SuppressWarnings("unchecked")
   public static List<FileItem> getUploadItems(HttpServletRequest request, boolean useLargeFileSize,
    String tempDirNameInput) throws FileUploadException, Exception {

int max_size = UploadFileUtil.DEFAULT_MAX_SIZE;
int max_memory_size;
String tempDirName = null;

int tempInt = -1;

if (useLargeFileSize) {
    tempInt = Configuration.getAsInt(ConfigurationKeys.UPLOAD_FILE_LARGE_MAX_SIZE);
    if (tempInt != -1) {
    max_size = tempInt;
    } else {
    UploadFileUtil.log.warn(
        "Default Large Max Size for file upload missing, using " + UploadFileUtil.DEFAULT_MAX_SIZE);
    }
} else {
    tempInt = Configuration.getAsInt(ConfigurationKeys.UPLOAD_FILE_MAX_SIZE);
    if (tempInt != -1) {
    max_size = tempInt;
    } else {
    UploadFileUtil.log
        .warn("Default Max Size for file upload missing, using " + UploadFileUtil.DEFAULT_MAX_SIZE);
    }
}

tempInt = Configuration.getAsInt(ConfigurationKeys.UPLOAD_FILE_MAX_MEMORY_SIZE);
if (tempInt != -1) {
    max_memory_size = tempInt;
} else {
    UploadFileUtil.log.warn(
        "Default Max Memory Size for file upload missing, using " + UploadFileUtil.DEFAULT_MEMORY_SIZE);
    max_memory_size = UploadFileUtil.DEFAULT_MEMORY_SIZE;
}

if (tempDirNameInput != null) {
    tempDirName = tempDirNameInput;
} else {
    tempDirName = Configuration.get(ConfigurationKeys.LAMS_TEMP_DIR);
    if (tempDirName == null) {
    UploadFileUtil.log.warn("Default Temporary Directory missing, using null");
    }
}
// would be nice to only do this once! never mind.
if (tempDirName != null) {
    File dir = new File(tempDirName);
    if (!dir.exists()) {
    dir.mkdirs();
    }
}

// Create a new file upload handler
DiskFileUpload upload = new DiskFileUpload();

// Set upload parameters
upload.setSizeMax(max_size);
upload.setSizeThreshold(max_memory_size);
upload.setRepositoryPath(tempDirName);

// Parse the request
List<FileItem> items = upload.parseRequest(request);
return items;
   }
项目:lams    文件:QuestionsAction.java   
@SuppressWarnings("unchecked")
   @Override
   public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
    HttpServletResponse response) throws Exception {
// cumberstone way of extracting POSTed form with a file
DiskFileUpload formParser = new DiskFileUpload();
formParser.setRepositoryPath(Configuration.get(ConfigurationKeys.LAMS_TEMP_DIR));
List<FileItem> formFields = formParser.parseRequest(request);

String returnURL = null;
String limitTypeParam = null;
InputStream uploadedFileStream = null;
String packageName = null;
for (FileItem formField : formFields) {
    String fieldName = formField.getFieldName();
    if ("returnURL".equals(fieldName)) {
    // this can be empty; if so, another method of delivering results is used
    returnURL = formField.getString();
    } else if ("limitType".equals(fieldName)) {
    limitTypeParam = formField.getString();
    } else if ("file".equals(fieldName) && !StringUtils.isBlank(formField.getName())) {
    packageName = formField.getName().toLowerCase();
    uploadedFileStream = formField.getInputStream();
    }
}

// this parameter is not really used at the moment
request.setAttribute("returnURL", returnURL);

// show only chosen types of questions
request.setAttribute("limitType", limitTypeParam);

// user did not choose a file
if ((uploadedFileStream == null) || !(packageName.endsWith(".zip") || packageName.endsWith(".xml"))) {
    ActionMessages errors = new ActionMessages();
    errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("label.questions.file.missing"));
    request.setAttribute(Globals.ERROR_KEY, errors);
    return mapping.findForward("questionFile");
}

Set<String> limitType = null;
if (!StringUtils.isBlank(limitTypeParam)) {
    limitType = new TreeSet<String>();
    // comma delimited acceptable question types, for example "mc,fb"
    Collections.addAll(limitType, limitTypeParam.split(","));
}

Question[] questions = packageName.endsWith(".xml")
    ? QuestionParser.parseQTIFile(uploadedFileStream, null, limitType)
    : QuestionParser.parseQTIPackage(uploadedFileStream, limitType);
request.setAttribute("questions", questions);

return mapping.findForward("questionChoice");
   }
项目:sistra    文件:IniciFormServlet.java   
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        log("Iniciar tramitaci�");

        String xmlData = "";
        String xmlConfig = "";

        if (FileUpload.isMultipartContent(request)) {
            try {
                DiskFileUpload fileUpload = new DiskFileUpload();
                List fileItems = fileUpload.parseRequest(request);

                for (int i = 0; i < fileItems.size(); i++) {
                    FileItem fileItem = (FileItem) fileItems.get(i);
                    if (fileItem.getFieldName().equals("xmlData")) {
                        xmlData = fileItem.getString();
                    }

                    if (fileItem.getFieldName().equals("xmlConfig")) {
                        xmlConfig = fileItem.getString();
                    }
                }
            } catch (FileUploadException e) {
                throw new UnavailableException("Error uploading", 1);
            }
        } else {
            xmlData = request.getParameter("xmlData");
            xmlConfig = request.getParameter("xmlConfig");
        }

        log("XML Data: " + xmlData);
        log("XML Config: " + xmlConfig);

        log("Enviant dades a: " + urlTramitacio);

        String token = iniciarTramite(xmlData, xmlConfig);

        if (token == null) {
            log("Token �s null");
        } else {
            log("Token: " + token);

            StringBuffer url = new StringBuffer(urlRedireccio);
            url.append(urlRedireccio.indexOf('?') == -1 ? '?' : '&');
            url.append(tokenName);
            url.append('=');
            url.append(token);

            log("Redireccionant a: " + url.toString());
            response.sendRedirect(response.encodeRedirectURL(url.toString()));
        }
    }
项目:hermes    文件:AgreementUploadPageletAdaptor.java   
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();
    }
项目:subetha    文件:FileUploadFilter.java   
/**
 * Filter the current request. If it is a multipart request, parse it and
 * wrap it before chaining to the next filter or servlet. Otherwise, pass
 * it on untouched.
 */
@Override
@SuppressWarnings("unchecked")
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
    throws IOException, ServletException
{
    if (!FileUploadBase.isMultipartContent(request))
    {
        chain.doFilter(request, response);
        return;
    }

    try
    {
        //FileItemFactory fact = new DiskFileItemFactory();
        //FileUpload upload = new ServletFileUpload(fact);
        DiskFileUpload upload = new DiskFileUpload();
        upload.setSizeMax(MAX_UPLOAD_BYTES);

        List<FileItem> items = upload.parseRequest(request);

        Map<String, String[]> params = new HashMap<String, String[]>();
        List<FileItem> files = new ArrayList<FileItem>(items.size());

        for (FileItem item: items)
        {
            if (item.isFormField())
            {
                // Add it to the array in the params, creating the array if necessary
                String[] array = params.get(item.getFieldName());
                if (array == null)
                {
                    array = new String[] { item.getString() };
                }
                else
                {
                    String[] newArray = new String[array.length + 1];
                    System.arraycopy(array, 0, newArray, 0, array.length);
                    newArray[newArray.length - 1] = item.getString();

                    array = newArray;
                }

                params.put(item.getFieldName(), array);
            }
            else
                files.add(item);
        }

        request.setAttribute(ATTR_FILES, files);
        HttpServletRequest wrapped = new RequestWrapper(request, params);

        chain.doFilter(wrapped, response);
    }
    catch (FileUploadException ex)
    {
        // Just save the exception for later.
        request.setAttribute(ATTR_EXCEPTION, ex);

        chain.doFilter(request, response);
    }
}