Struts2 文件上传
效果:
案例:
在targer里面加一个 文件夹 upload 这是我们上传文件的路径
最好我们先在里面放点文件 不然 上传的时候容易报错
1.引入jar包
我们需要格外引入2和commons的jar
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
2.Action类:
private String uploadContentType;
private String uploadFileName;
这2个属性名 必须要以ContentType 和 FileName 结尾
package cn.happy.struts09file;
import com.opensymphony.xwork2.Action;
import org.apache.struts2.ServletActionContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* Created by linlin on 2017/10/30.
*/
public class FileAction implements Action{
private File upload;
private String uploadContentType;
private String uploadFileName;
private String savePath;
public String execute() throws Exception {
byte[] buffer=new byte[1024];
FileInputStream fis=new FileInputStream(getUpload());
FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+getUploadFileName());
int length=fis.read(buffer);
while (length>0){
fos.write(buffer,0,length);
length=fis.read(buffer);
}
fos.flush();
fos.close();
fis.close();
return SUCCESS;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getSavePath() {
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
3.struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="day-09" namespace="" extends="struts-default">
<action name="uploadAction" class="cn.happy.struts09file.FileAction">
<param name="savePath">/upload</param>
<result name="success">/file/strutsupload.jsp</result>
</action>
</package>
</struts>
4.页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Struts2文件上传</title>
</head>
<body>
<s:form action="uploadAction" enctype="multipart/form-data" method="post">
<s:file name="upload" label="选择文件"/><br/>
<s:submit name="submit" value="文件上传"/>
</s:form>
</body>
</html>
2.
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Struts2文件上传页面</title>
</head>
<body>
文件是: <s:property value="uploadFileName"></s:property><br/>
文件类型:<s:property value="uploadContentType"></s:property><br/>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:struts.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
启动项目 。。。。