1、导入struts包
2、配置web.xml
- <?xmlversion="1.0"encoding="UTF-8"?>
- <web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- id="WebApp_ID"version="2.5">
- <display-name>NewJava</display-name>
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
3、上传JSP页面
- <%@pagelanguage="java"contentType="text/html;charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@taglibprefix="s"uri="/struts-tags"%>
- <!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
- <title>文件上传</title>
- </head>
- <body>
- <divalign="center">
- <br/><br/>
- <imgalt="文件上传"src="images/upload.png">
- <s:formmethod="post"action="Upload"theme="simple"enctype="multipart/form-data">
- 文件名称:<s:textfieldname="title"></s:textfield>
- 文件地址:<s:filename="upload"></s:file>
- <s:submitvalue="上传"></s:submit>
- </s:form>
- </div>
- </body>
- </html>
4、编写Action
- packagecom.action;
- importjava.io.File;
- importjava.io.FileInputStream;
- importjava.io.FileOutputStream;
- importjavax.servlet.http.HttpServletRequest;
- importorg.apache.struts2.ServletActionContext;
- importcom.opensymphony.xwork2.ActionSupport;
- publicclassUploadActionextendsActionSupport{
- privateStringtitle;
- privateFileupload;
- privateStringuploadContentType;
- //封装上传文件名的属性
- privateStringuploadFileName;
- //直接在struts.xml文件中配置的属性
- publicStringexecute()throwsException{
- //以服务器的文件保存地址和原文件名建立上传文件输出流
- Stringpath=ServletActionContext.getServletContext().getRealPath("/load/");//获取服务器存放物理路径
- System.out.println(path+getUploadFileName());
- FileOutputStreamfos=newFileOutputStream(path+"\\"+getUploadFileName());
- FileInputStreamfis=newFileInputStream(getUpload());
- byte[]buffer=newbyte[1024];
- intlen=0;
- while((len=fis.read(buffer))>0)
- {fos.write(buffer,0,len);
- }
- System.out.println("上传成功");
- System.out.println(path+"\\"+getUploadFileName());
- returnSUCCESS;
- }
- publicStringgetTitle(){
- returntitle;
- }
- publicvoidsetTitle(Stringtitle){
- this.title=title;
- }
- publicFilegetUpload(){
- returnupload;
- }
- publicvoidsetUpload(Fileupload){
- this.upload=upload;
- }
- publicStringgetUploadContentType(){
- returnuploadContentType;
- }
- publicvoidsetUploadContentType(StringuploadContentType){
- this.uploadContentType=uploadContentType;
- }
- publicStringgetUploadFileName(){
- returnuploadFileName;
- }
- publicvoidsetUploadFileName(StringuploadFileName){
- this.uploadFileName=uploadFileName;
- }
- }
5、配置struts.xml
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEstrutsPUBLIC
- "-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <constantname="struts.enable.DynamicMethodInvocation"value="true"/>
- <constantname="struts.devMode"value="true"/>
- <packagename="example"extends="struts-default">
- <actionname="Upload"class="com.action.UploadAction">
- <result>/download.jsp</result>
- </action>
- </package>
- </struts>
6、看看目录结构 运行吧 文件上传到load文件夹下面 可能有点慢
1、导入struts包
2、配置web.xml
- <?xmlversion="1.0"encoding="UTF-8"?>
- <web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- id="WebApp_ID"version="2.5">
- <display-name>NewJava</display-name>
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
3、上传JSP页面
- <%@pagelanguage="java"contentType="text/html;charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@taglibprefix="s"uri="/struts-tags"%>
- <!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
- <title>文件上传</title>
- </head>
- <body>
- <divalign="center">
- <br/><br/>
- <imgalt="文件上传"src="images/upload.png">
- <s:formmethod="post"action="Upload"theme="simple"enctype="multipart/form-data">
- 文件名称:<s:textfieldname="title"></s:textfield>
- 文件地址:<s:filename="upload"></s:file>
- <s:submitvalue="上传"></s:submit>
- </s:form>
- </div>
- </body>
- </html>
4、编写Action
- packagecom.action;
- importjava.io.File;
- importjava.io.FileInputStream;
- importjava.io.FileOutputStream;
- importjavax.servlet.http.HttpServletRequest;
- importorg.apache.struts2.ServletActionContext;
- importcom.opensymphony.xwork2.ActionSupport;
- publicclassUploadActionextendsActionSupport{
- privateStringtitle;
- privateFileupload;
- privateStringuploadContentType;
- //封装上传文件名的属性
- privateStringuploadFileName;
- //直接在struts.xml文件中配置的属性
- publicStringexecute()throwsException{
- //以服务器的文件保存地址和原文件名建立上传文件输出流
- Stringpath=ServletActionContext.getServletContext().getRealPath("/load/");//获取服务器存放物理路径
- System.out.println(path+getUploadFileName());
- FileOutputStreamfos=newFileOutputStream(path+"\\"+getUploadFileName());
- FileInputStreamfis=newFileInputStream(getUpload());
- byte[]buffer=newbyte[1024];
- intlen=0;
- while((len=fis.read(buffer))>0)
- {fos.write(buffer,0,len);
- }
- System.out.println("上传成功");
- System.out.println(path+"\\"+getUploadFileName());
- returnSUCCESS;
- }
- publicStringgetTitle(){
- returntitle;
- }
- publicvoidsetTitle(Stringtitle){
- this.title=title;
- }
- publicFilegetUpload(){
- returnupload;
- }
- publicvoidsetUpload(Fileupload){
- this.upload=upload;
- }
- publicStringgetUploadContentType(){
- returnuploadContentType;
- }
- publicvoidsetUploadContentType(StringuploadContentType){
- this.uploadContentType=uploadContentType;
- }
- publicStringgetUploadFileName(){
- returnuploadFileName;
- }
- publicvoidsetUploadFileName(StringuploadFileName){
- this.uploadFileName=uploadFileName;
- }
- }
5、配置struts.xml
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEstrutsPUBLIC
- "-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <constantname="struts.enable.DynamicMethodInvocation"value="true"/>
- <constantname="struts.devMode"value="true"/>
- <packagename="example"extends="struts-default">
- <actionname="Upload"class="com.action.UploadAction">
- <result>/download.jsp</result>
- </action>
- </package>
- </struts>
6、看看目录结构 运行吧 文件上传到load文件夹下面 可能有点慢