问题描述
我用struts2 做的文件上传1.前台imgUp.ftl代码为:<form action="upload.action" method="post" enctype="multipart/form-data"><div id="upforms"><div class="upfileDesc">选择类型:<select name="selectDir" class="select1" ><option value="a">a文件夹</option><option value="b">b文件夹</option></select></div><div class="upfroms1"><input type="file" name="upload" /></div><div class="upregedit_submit"><input type="submit" value="上传"/></div><div class="upfileDesc"><span class="">只能上传jpg,gif类型图片,大小不能超过5M</span></div><div class="upfileDesc"><span class="">${fieldErrors['fileUp']!}</span></div></div></form>2.action代码为:public class UploadAction extends BasePageSupport{private String selectDir;//上传目录参数private File upload;//上传文件域private String uploadContentType;//上传文件的类型private String uploadFileName;//上传文件名public String getSelectDir() {return selectDir;}public void setSelectDir(String selectDir) {this.selectDir = selectDir;}public File getUpload() {return upload;}public void setUpload(File upload) {this.upload = upload;}public String getUploadContentType() {return uploadContentType;}public void setUploadContentType(String uploadContentType) {this.uploadContentType = uploadContentType;}public String getUploadFileName() {return uploadFileName;}public void setUploadFileName(String uploadFileName) {this.uploadFileName = uploadFileName;}/** * 文件上传入口 * * @return String * @throws Exception */public String toUpload() throws Exception {return SUCCESS;}/** * 文件上传 * * @return String * @throws Exception */public String uploadFile() throws Exception{System.out.println("文件名="+getUploadFileName());System.out.println("文件类型="+getUploadContentType());System.out.println("上传目录="+getSelectDir());return SUCCESS;}}3.struts.xml配置<!-- 配置解析请求参数时所用的解码集 --><constant name="struts.custom.i18n.resources" value="globalMessages"/><constant name="struts.i18n.encoding" value="GBK"/><package name="upload" namespace="/" extends="xtiger-default"><!-- 文件上传入口 --><action name="toUpload" class="com.comabc.web.action.front.UploadAction" method="toUpload"> <result> <param name="location">/WEB-INF/template/front/imgUp.ftl</param> </result></action><!-- 文件上传 --><action name="upload" class="com.comabc.web.action.front.UploadAction" method="uploadFile"><result name="success"><param name="location">/WEB-INF/template/front/imgUp.ftl</param></result><result name="input"><param name="location">/WEB-INF/template/front/imgUp.ftl</param></result></action></package>4.web.xml配置还是以前一样的: <!-- 定义struts2的filterDispathcer的filter --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <!-- filterDispatcher用来初始化struts2并且处理所有的web请求 --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <!-- FilterDispatcher辅助类开始,多文件上传时用 --><filter> <filter-name> struts-cleanup </filter-name> <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class> </filter> <filter-mapping> <filter-name>struts-cleanup</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- FilterDispatcher辅助类结束 -->大家帮我看看。哪里不对吗?我运行。输出总是:文件名=null文件类型=null上传目录=a文件名和文件类型都接收不到!
解决方案
JSP没什么问题,问题出在UploadAction 中。你的上传文件类型和文件名根本就没有赋值当然为空了。……FormFile file = form.getImportFile();……然后从file 中获取文件信息给变量private String uploadContentType;//上传文件的类型 private String uploadFileName;//上传文件名 赋值。另外JSP中我修改一下:<html:file property="upload" />要用到apache的 commons-fileupload.jar包,struts自带有这个包。