文件上传问题

rushly 2011-05-25 02:05:30
1.我在webwork.properties中限制了文件上传的大小,那如何在程序中去控制文件的大小,也就是当文件上传的大小超过限制以后如何去返回给前台一个出错信息,而不是直接抛出异常?重载fileUpload?还是什么其它方法?
2.上传超过限制大小时,FileUploadInterceptor拦截器 不报错,而返回到自己写的action中时 相关file的属性都为空。包空指针错误。怎么才能把超过大小的错误在前台页面显示呢?
...全文
226 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
SuiJiyuan 2013-03-14
  • 打赏
  • 举报
回复
请问楼主如何解决fileParameterNames.hasMoreElements()返回false的问题? 可否将解决方案发到我的邮箱里,谢谢:adler.sui@gmail.com
rushly 2011-05-26
  • 打赏
  • 举报
回复
求高手关注,,求指教,,急求
rushly 2011-05-26
  • 打赏
  • 举报
回复
我设了60M

webwork.properties里
webwork.multipart.maxSize = 62914560

xwork.xml里也设了
<interceptor-stack name="fileUpload-validation-stack">
<interceptor-ref name="fileUpload">
<param name="maximumSize">62914560</param>
</interceptor-ref>


才50M文件为什么
  public String intercept(ActionInvocation invocation) throws Exception {
ActionContext ac = invocation.getInvocationContext();
HttpServletRequest request = (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);

if (!(request instanceof MultiPartRequestWrapper)) {
if (log.isDebugEnabled()) {
ActionProxy proxy = invocation.getProxy();
log.debug(getTextMessage("webwork.messages.bypass.request", new Object[]{proxy.getNamespace(), proxy.getActionName()}, ActionContext.getContext().getLocale()));
}

return invocation.invoke();
}

final Object action = invocation.getAction();
ValidationAware validation = null;

if (action instanceof ValidationAware) {
validation = (ValidationAware) action;
}

MultiPartRequestWrapper multiWrapper = (MultiPartRequestWrapper) request;

if (multiWrapper.hasErrors()) {
for (Iterator errorIter = multiWrapper.getErrors().iterator(); errorIter.hasNext();) {
String error = (String) errorIter.next();

if (validation != null) {
validation.addActionError(error);
}

log.error(error);
}
}

Map parameters = ac.getParameters();

// Bind allowed Files
Enumeration fileParameterNames = multiWrapper.getFileParameterNames();
while (fileParameterNames != null && fileParameterNames.hasMoreElements()) {
// get the value of this input tag
String inputName = (String) fileParameterNames.nextElement();

// get the content type
String[] contentType = multiWrapper.getContentTypes(inputName);

if (isNonEmpty(contentType)) {
// get the name of the file from the input tag
String[] fileName = multiWrapper.getFileNames(inputName);

if (isNonEmpty(fileName)) {
// Get a File object for the uploaded File
File[] files = multiWrapper.getFiles(inputName);
if (files != null) {
for (int index = 0; index < files.length; index++) {
getTextMessage("webwork.messages.current.file", new Object[]{inputName, contentType[index], fileName[index], files[index]}, ActionContext.getContext().getLocale());

if ( acceptFile(files[index], contentType[index], inputName, validation, ac.getLocale())) {
parameters.put(inputName, files);
parameters.put(inputName + "ContentType", contentType);
parameters.put(inputName + "FileName", fileName);
}
}
}
} else {
log.error(getTextMessage("webwork.messages.invalid.file", new Object[]{inputName}, ActionContext.getContext().getLocale()));
}
} else {
log.error(getTextMessage("webwork.messages.invalid.content.type", new Object[]{inputName}, ActionContext.getContext().getLocale()));
}
}

// invoke action
String result = invocation.invoke();

// cleanup
fileParameterNames = multiWrapper.getFileParameterNames();
while (fileParameterNames != null && fileParameterNames.hasMoreElements()) {
String inputValue = (String) fileParameterNames.nextElement();
File[] file = multiWrapper.getFiles(inputValue);
for (int index = 0; index < file.length; index++) {
File currentFile = file[index];
log.info(getTextMessage("webwork.messages.removing.file", new Object[]{inputValue, currentFile}, ActionContext.getContext().getLocale()));

if ((currentFile != null) && currentFile.isFile()) {
currentFile.delete();
}
}
}

return result;
}

在我重写的拦截器中
  while (fileParameterNames != null && fileParameterNames.hasMoreElements()) {
这一句就走不下去了(request里的参数都变成null的原因)
yangwawa19870921 2011-05-26
  • 打赏
  • 举报
回复
<s:file name="upfile" value="图片上传" />
<s:fielderror fieldName="upfile" /> //在此处显示错误信息
在action.properties文件中写struts.messages.error.file.too.large=\u4E0A\u4F20\u7684\u6587\u4EF6\u592A\u5927 即页面提示“上传文件太大”
lovemichael2 2011-05-26
  • 打赏
  • 举报
回复
用xwork校验一下
哈哈哈3234 2011-05-26
  • 打赏
  • 举报
回复
关注哦 了
rushly 2011-05-26
  • 打赏
  • 举报
回复
请看清题目,,多谢关注
吸尘器 2011-05-26
  • 打赏
  • 举报
回复
可以扑捉异常,然后在request中储存"上传文件过大",在前台弹个框好啦
凯瑞甘 2011-05-26
  • 打赏
  • 举报
回复
你百度 啥都有了
rushly 2011-05-26
  • 打赏
  • 举报
回复
多谢关注
凯瑞甘 2011-05-26
  • 打赏
  • 举报
回复
//取得文件
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
//取得文件名
String fileName = myFile.getFileName();
//取得文件大小
int fileSize = myFile.getSize();
休谱诺斯 2011-05-26
  • 打赏
  • 举报
回复
ajax吧。。。
rushly 2011-05-26
  • 打赏
  • 举报
回复
为什么出现错误 在action中获取 getFileError getFileErrorMaping ;getErrorMesg...都是空了,,搞不懂 急求答案,在线等
rushly 2011-05-26
  • 打赏
  • 举报
回复
还有其他方法?jsp页面能获取用户选中文件大小就好了 呵呵
rushly 2011-05-26
  • 打赏
  • 举报
回复
已解决 来者有份 哈哈,,欢迎提看法意见 和 设计方式
走位 2011-05-26
  • 打赏
  • 举报
回复
呵呵 遇到同样问题明明没超过最大限制 还是在拦截器的
while (fileParameterNames != null && fileParameterNames.hasMoreElements())
这一句为false。
为什么呢,,难道xwork上传本身就有限制? 同求答案 谢谢
rushly 2011-05-26
  • 打赏
  • 举报
回复
急 求
lixin5678 2011-05-25
  • 打赏
  • 举报
回复
实现ServletRequestAware或者SessionAware接口,
在拦截器中把信息写到request或者session的MAP中,
然后在JSP页面中用表达式requestScope.XXXX或者
sessionScope.XXXX取出来就行了。
wantenliang 2011-05-25
  • 打赏
  • 举报
回复
阿发嘎嘎噶

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧