eclipse java文件中ServletActionContext.getServletContext()报找不到getServletContext()方法

在eclipse中ServletActionContext.getServletContext()报找不到getServletContext()方法。

 

我在我导入的struts2-core.jar中找到了getServletContext()但还是提示ServletActionContext里没有getServletContext()方法。后来经过搜索才知道原来是少了servlet-api架包,导入servlet-api.jar既可解决!

转载于:https://www.cnblogs.com/zengsong-restService/archive/2013/04/08/3006692.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
public class UploadAction extends ActionSupport { //定义一个继承自ActionSupport的类UploadAction,用于文件上传功能 private static final long serialVersionUID = 1L; //定义一个序列化ID,用于版本控制 private static final String CONTENT_TYPE= "text/html; charset=utf-8"; //定义一个常量CONTENT_TYPE,表示上传文件的内容类型 public String execute() { //定义一个execute方法,用于执行文件上传操作 HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE); //获取HttpServletResponse响应对象 HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_REQUEST); //获取HttpServletRequest请求对象 SingleFileUpload upload = new SingleFileUpload(); //创建SingleFileUpload类的实例 try { upload.parseRequest(request); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } //解析HttpServletRequest请求对象,获取上传的文件信息 String temp =ServletActionContext.getServletContext().getRealPath("/") + "upload\temp\"; //获取临时目录存储路径 System.out.print("上传路径"+temp); //输出上传路径 String loadpath = ServletActionContext.getServletContext().getRealPath("/") + "upload\"; //获取上传文件存储路径 File file = new File(temp); //创建文件对象 if(!file.exists()) file.mkdirs(); //如果临时目录不存在,则创建该目录 try { upload.upload(file); response.getWriter().write(upload.getFileItem().getName()); } catch(org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException e){ //文件大小超过限制 e.printStackTrace(); }catch (Exception e){ e.printStackTrace(); } //上传文件,将文件存储到指定路径下,返回文件名 return null; //返回null值,表示上传完成,不进行页面跳转 } } //类UploadAction结束

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值