本文实例为大家分享了Struts2框架实现文件上传的方法,供大家参考,具体内容如下
struts2的配置过程
(1)在项目中加入jar包
(2)web.xml中filter(过滤器)的配置
struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 public String execute() throws IOException{ System.out.println(title);//标题 System.out.println(uploadContentType);//准备上传的文件的文件类型 System.out.println(uploadFileName);//准备上传的文件的文件名,记住还有扩展名 System.out.println(upload); String realPath=ServletActionContext.getServletContext().getRealPath("/"); String path=realPath+"myupload/"+uploadFileName; System.out.println(realPath); System.out.println(path); FileInputStream fis=new FileInputStream(upload); FileOutputStream fos=new FileOutputStream(path); byte[] bytes=new byte[1024];//定义一个1024大小的字节数组 int len=-1;//用来做标志位 while((len=fis.read(bytes))>0){ fos.write(bytes, 0, len); } return null; } }
(5)JSP页面的编写
<%@ page contentType="text/html;charset=utf-8"%>
经过这次总结,感觉struts2框架下的单文件的上传还是非常简单的,只要获取要存储在什么地方的地址,再通过输入输出流,写到这个地址中去就行了。绝大部分工作,struts2已经帮我们做好了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



