本文整理汇总了Java中org.apache.commons.fileupload.DefaultFileItemFactory类的典型用法代码示例。如果您正苦于以下问题:Java DefaultFileItemFactory类的具体用法?Java DefaultFileItemFactory怎么用?Java DefaultFileItemFactory使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DefaultFileItemFactory类属于org.apache.commons.fileupload包,在下文中一共展示了DefaultFileItemFactory类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getFileUploadContent
import org.apache.commons.fileupload.DefaultFileItemFactory; //导入依赖的package包/类
public static ClassifiableContentIF getFileUploadContent(HttpServletRequest request) {
// Handle file upload
String contentType = request.getHeader("content-type");
// out.write("CT: " + contentType + " " + tm + " " + id);
if (contentType != null && contentType.startsWith("multipart/form-data")) {
try {
FileUpload upload = new FileUpload(new DefaultFileItemFactory());
for (FileItem item : upload.parseRequest(request)) {
if (item.getSize() > 0) {
// ISSUE: could make use of content type if known
byte[] content = item.get();
ClassifiableContent cc = new ClassifiableContent();
String name = item.getName();
if (name != null)
cc.setIdentifier("fileupload:name:" + name);
else
cc.setIdentifier("fileupload:field:" + item.getFieldName());
cc.setContent(content);
return cc;
}
}
} catch (Exception e) {
throw new OntopiaRuntimeException(e);
}
}
return null;
}
示例2: execute
import org.apache.commons.fileupload.DefaultFileItemFactory; //导入依赖的package包/类
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
Logger logger = MiscUtils.getLogger();
String simulationData = null;
boolean simulationError = false;
try {
FileUpload upload = new FileUpload(new DefaultFileItemFactory());
@SuppressWarnings("unchecked")
List<FileItem> items = upload.parseRequest(request);
for(FileItem item:items) {
if(item.isFormField()) {
String name = item.getFieldName();
if(name.equals("simulateError")) {
simulationError=true;
}
} else {
if(item.getFieldName().equals("simulateFile")) {
InputStream is = item.getInputStream();
StringWriter writer = new StringWriter();
IOUtils.copy(is, writer, "UTF-8");
simulationData = writer.toString();
}
}
}
if(simulationData != null && simulationData.length()>0) {
if(simulationError) {
Driver.readResponseFromXML(request, simulationData);
simulationData = (String)request.getAttribute("olisResponseContent");
request.getSession().setAttribute("errors",request.getAttribute("errors"));
}
request.getSession().setAttribute("olisResponseContent", simulationData);
request.setAttribute("result", "File successfully uploaded");
}
}catch(Exception e) {
MiscUtils.getLogger().error("Error",e);
}
return mapping.findForward("success");
}
示例3: decodeParameters
import org.apache.commons.fileupload.DefaultFileItemFactory; //导入依赖的package包/类
/**
* INTERNAL: Builds the Parameters object from an HttpServletRequest
* object.
* @since 2.0
*/
public static Parameters decodeParameters(HttpServletRequest request,
String charenc)
throws ServletException, IOException {
String ctype = request.getHeader("content-type");
log.debug("Content-type: " + ctype);
Parameters params = new Parameters();
if (ctype != null && ctype.startsWith("multipart/form-data")) {
// special file upload request, so use FileUpload to decode
log.debug("Decoding with FileUpload; charenc="+ charenc);
try {
FileUpload upload = new FileUpload(new DefaultFileItemFactory());
Iterator iterator = upload.parseRequest(request).iterator();
while (iterator.hasNext()) {
FileItem item = (FileItem) iterator.next();
log.debug("Reading: " + item);
if (item.isFormField()) {
if (charenc != null)
params.addParameter(item.getFieldName(), item.getString(charenc));
else
params.addParameter(item.getFieldName(), item.getString());
} else
params.addParameter(item.getFieldName(), new FileParameter(item));
}
} catch (FileUploadException e) {
throw new ServletException(e);
}
} else {
// ordinary web request, so retrieve info and stuff into Parameters object
log.debug("Normal parameter decode, charenc=" + charenc);
if (charenc != null)
request.setCharacterEncoding(charenc);
Enumeration enumeration = request.getParameterNames();
while (enumeration.hasMoreElements()) {
String param = (String) enumeration.nextElement();
params.addParameter(param, request.getParameterValues(param));
}
}
return params;
}