代码改变世界

Struts2上传图片时报404错误

2014-04-25 14:43  合翅  阅读(398)  评论(0编辑  收藏  举报

可能是struts配置文件中定义的拦截器导致的,后缀拦截导致,将该拦截器去掉,在action类里判断后缀

public String upload()throws Exception{
ActionContext ctxActionContext = ActionContext.getContext();
//this.schoolar = this.expertService.findSchoolarByUid(uid) ;
String directory = ServletActionContext.getServletContext()
.getRealPath(savePath);
File file = new File(directory);
String fileTypesString = MessageUtil.get("fileTypes");// 获取属性文件中规定的附件的文件类型
String fileTypes[] = null;
fileTypes = fileTypesString.split(",");// 所有合法的文件类型后缀构成的字符数组

String suffix = null;// 上传的附件的后缀
suffix = this.getUploadFileName().substring(
this.getUploadFileName().lastIndexOf(".") + 1,
this.getUploadFileName().length());
// 循环判断 后缀是否合法
int count = 0;// 计数器
for (int i = 0; i < fileTypes.length; i++) {
if (fileTypes[i].equalsIgnoreCase(suffix)) {
break;
} else {
count++;
}
}
if (count == fileTypes.length) {
ctxActionContext.put("msg", "文件类型不合法,请核对类型!");
return INPUT;
}
//......
}