struts 如何获取 file 类型数据

[b]看了STRUTS源代码
怎么也没看到 ActionForm 里面的
MultipartRequestHandler multipartRequestHandler
如何获取到到request的数据的代码[/b]

以下为RequestUtils拷贝属性方法,忘大侠们指点一二:

public static void populate(Object bean, String prefix, String suffix,
HttpServletRequest request)
throws ServletException {
// Build a list of relevant request parameters from this request
HashMap properties = new HashMap();

// Iterator of parameter names
Enumeration names = null;

// Map for multipart parameters
Map multipartParameters = null;

String contentType = request.getContentType();
String method = request.getMethod();
boolean isMultipart = false;

if (bean instanceof ActionForm) {
((ActionForm) bean).setMultipartRequestHandler(null);
}

MultipartRequestHandler multipartHandler = null;
if ((contentType != null)
&& (contentType.startsWith("multipart/form-data"))
&& (method.equalsIgnoreCase("POST"))) {
// Get the ActionServletWrapper from the form bean
ActionServletWrapper servlet;

if (bean instanceof ActionForm) {
servlet = ((ActionForm) bean).getServletWrapper();
} else {
throw new ServletException("bean that's supposed to be "
+ "populated from a multipart request is not of type "
+ "\"org.apache.struts.action.ActionForm\", but type "
+ "\"" + bean.getClass().getName() + "\"");
}

// Obtain a MultipartRequestHandler
multipartHandler = getMultipartHandler(request);

if (multipartHandler != null) {
isMultipart = true;

// Set servlet and mapping info
servlet.setServletFor(multipartHandler);
multipartHandler.setMapping((ActionMapping) request
.getAttribute(Globals.MAPPING_KEY));

// Initialize multipart request class handler
multipartHandler.handleRequest(request);

//stop here if the maximum length has been exceeded
Boolean maxLengthExceeded =
(Boolean) request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);

if ((maxLengthExceeded != null)
&& (maxLengthExceeded.booleanValue())) {
((ActionForm) bean).setMultipartRequestHandler(multipartHandler);
return;
}

//retrieve form values and put into properties
multipartParameters =
getAllParametersForMultipartRequest(request,
multipartHandler);
names = Collections.enumeration(multipartParameters.keySet());
}
}

if (!isMultipart) {
names = request.getParameterNames();
}

while (names.hasMoreElements()) {
String name = (String) names.nextElement();
String stripped = name;

if (prefix != null) {
if (!stripped.startsWith(prefix)) {
continue;
}

stripped = stripped.substring(prefix.length());
}

if (suffix != null) {
if (!stripped.endsWith(suffix)) {
continue;
}

stripped =
stripped.substring(0, stripped.length() - suffix.length());
}

Object parameterValue = null;

if (isMultipart) {
parameterValue = multipartParameters.get(name);
} else {
parameterValue = request.getParameterValues(name);
}

// Populate parameters, except "standard" struts attributes
// such as 'org.apache.struts.action.CANCEL'
if (!(stripped.startsWith("org.apache.struts."))) {
properties.put(stripped, parameterValue);
}
}

// Set the corresponding properties of our bean
try {
BeanUtils.populate(bean, properties);
} catch (Exception e) {
throw new ServletException("BeanUtils.populate", e);
} finally {
if (multipartHandler != null) {
// Set the multipart request handler for our ActionForm.
// If the bean isn't an ActionForm, an exception would have been
// thrown earlier, so it's safe to assume that our bean is
// in fact an ActionForm.
((ActionForm) bean).setMultipartRequestHandler(multipartHandler);
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值