struts2 注解 文件上传问题。

u014490157 2015-09-08 11:25:57
需求:
上传一个文件,批量导入数据库,返回结果(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}";
}


}

...全文
172 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
小凡没有笑 2015-09-08
  • 打赏
  • 举报
回复
要给xslfile 添加 get/set 方法,而且方法名必须是 getXxxFile / SetXxxFile 这种格式(Xxx为表单中文件标签的name)

81,122

社区成员

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

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