struts2 注解 文件上传有关问题
struts2 注解 文件上传问题。
需求:
上传一个文件,批量导入数据库,返回结果(json格式)给前台。
dao,services都已经写好。
由于对struts不熟悉,action接受文件流始终搞不定,有没有人指导一下?
------解决思路----------------------
要给xslfile 添加 get/set 方法,而且方法名必须是 getXxxFile / SetXxxFile 这种格式(Xxx为表单中文件标签的name)
需求:
上传一个文件,批量导入数据库,返回结果(json格式)给前台。
dao,services都已经写好。
由于对struts不熟悉,action接受文件流始终搞不定,有没有人指导一下?
@Controller
@ParentPackage("json-default")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Namespace("/userinfo")
public class ImportUserAction extends ActionSupport{
@Resource
private UserInfoService userInfoService;
// 封装上传文件域的属性
private File xslfile;
/**
* 用户信息批量导入
*/
@Action(value = "importUser",
results = { @Result(name = "success", type = "json") },
params = { "contentType", "text/html" })
public String execute() {
try {
userInfoService.importUser(new FileInputStream(xslfile));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("导入失败");
return "{success:true}";
}
System.out.println("导入成功");
return "{success:true}";
}
}
------解决思路----------------------
要给xslfile 添加 get/set 方法,而且方法名必须是 getXxxFile / SetXxxFile 这种格式(Xxx为表单中文件标签的name)