struts2上传文件问题

tntsuifeng 2010-09-13 05:17:32
JSP 页面部分

<form name="upload" action="upload.action" method="post" enctype="multipart/form-data">
<table width="99%" border="0" align="center" cellpadding="0" cellspacing="0" class="bordercol_01">
<tr>
<td class="coltitle_01"></td>
</tr>
<tr>
<td height="40" align="center"><table width="90%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="30%"> <input type="file" name="excelFile" /> </td>
<td width="70%"><input class="submit_01" type="submit" value="上 传" /></td>
<!--<td width="30%"><input type="text" name="name" label="username" ></td> -->
</tr>
</table>
</form>


struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<constant name="struts.devMode" value="true" />

<package name="default" namespace="/" extends="struts-default">
<action name="upload" class="com.wondersgroup.jxkh.dataimport.web.DataImpAction" method="uplodaFile">
<result name="success" >wel.jsp</result>
<result name="input" >wel.jsp</result>
</action>
</package>


</struts>



Action:


package com.wondersgroup.jxkh.dataimport.web;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;
import com.wondersgroup.jxkh.dataimport.bo.FileInfo;
import com.wondersgroup.jxkh.dataimport.service.DataImpService;

public class DataImpAction extends ActionSupport {

private File excelFile;
private HttpServletRequest req;
private DataImpService service;

public String uplodaFile(){
if(this.excelFile == null)
return INPUT;
FileInfo fileInfo = new FileInfo();
fileInfo.setFile(excelFile);
fileInfo.setFileName(excelFile.getName());

if(service.excelUpload(this.req,fileInfo)){
return SUCCESS;
}else{
return INPUT;
}
}


public File getFile() {
return excelFile;
}

public void setFile(File file) {
this.excelFile = file;
}

public HttpServletRequest getReq() {
return req;
}

public void setReq(HttpServletRequest req) {
this.req = req;
}

public DataImpService getService() {
return service;
}

public void setService(DataImpService service) {
this.service = service;
}

}


文件提交之后进到action里面,然后获取到的excelFile总之为null。
找了大半天没有搞清楚怎么回事,大家帮忙看下,谢谢了
...全文
88 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
weiyangkun 2010-09-13
  • 打赏
  • 举报
回复
private File excelFile;
没有gett/sett方法
Qautumn 2010-09-13
  • 打赏
  • 举报
回复
set/get方法一般都用自动生成的,你貌似是自己敲的吧。
a_b_a_b_a_b_a_b 2010-09-13
  • 打赏
  • 举报
回复
楼上正解
qqzhuyuehe 2010-09-13
  • 打赏
  • 举报
回复
public File getFile() {
return excelFile;
}

public void setFile(File file) {
this.excelFile = file;
}

这里的setter和getter不是随便写的,是有规则的,如下:

public File getExcelFile() {
return excelFile;
}

public void setExcelFile(File file) {
this.excelFile = file;
}

同理,其他的getter和setter方法也要类似的修改。

因为,在数据注入的时候,默认是要找相应的setter方法的。
tntsuifeng 2010-09-13
  • 打赏
  • 举报
回复
自己给顶上去了

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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