struts2实现文件上传

时间:2022-08-30 10:09:51

    一、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/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.4">
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<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>
</web-app>

   二、 upload.jsp

<%@ page language="java" contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>文件上传</title>
</head>
<body>
<center>
<h1>struts2文件上传</h1>
<form action="upload.action" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>上传文件:</td>
<td><input type="file" name="myUpload"/></td>
</tr>
<tr>
<td><input type="submit" value="上传"/></td>
<td><input type="reset" value="重置"/></td>
</tr>
</table>
</form>
</center>
</body>
</html>
    三、FileAction
package com.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.UUID;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileAction extends ActionSupport{
private String username;
private File myUpload;
private String myUploadFileName;
private String myUploadContentType;
private String savePath;
//接受struts.xml文件配置值的方法
public void setSavePath(String value){
this.savePath=value;
}
public String getSavePath() throws Exception{
String str=ServletActionContext.getServletContext().getRealPath(savePath);
return str;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public File getMyUpload() {
return myUpload;
}
public void setMyUpload(File myUpload) {
this.myUpload = myUpload;
}
public String getMyUploadFileName() {
return myUploadFileName;
}
public void setMyUploadFileName(String myUploadFileName) {
this.myUploadFileName = myUploadFileName;
}
public String getMyUploadContentType() {
return myUploadContentType;
}
public void setMyUploadContentType(String myUploadContentType) {
this.myUploadContentType = myUploadContentType;
}
public String upload() throws Exception{
String strNewFileName=UUID.randomUUID().toString();
String suffix=myUploadFileName.substring(myUploadFileName.lastIndexOf("."));
strNewFileName+=suffix;
FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+strNewFileName);
myUploadFileName=strNewFileName;
FileInputStream fis=new FileInputStream(getMyUpload());
byte[] buffer=new byte[1024];
int len=0;
while((len=fis.read(buffer))>0){
fos.write(buffer,0,len);
}
fos.close();
return SUCCESS;
}
}

   四、struts.xml

  

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd" >
<struts>
<package name="myPackage" namespace="/" extends="struts-default">
<action name="login" class="com.action.UserAction" method="execute">
<result>loginSuccess.jsp</result>
<result name="input">login.jsp</result>
</action>
<action name="upload" class="com.action.FileAction" method="upload">
<param name="savePath">/uploadFiles</param><!--配置文件中出现的保存文件的路径为uploadFiles,故应该在根目录下建立这样的一个文件夹 -->
<result name="success">/uploadSucc.jsp</result>
</action>
</package>
</struts>

  五、uploadSucc.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>上传成功</title>
</head>
<body>
上传成功!<br/>
用户名:<s:property value="username"/><br/><!--通过struts标签来布局,提高开发效率 -->
文件为:<img src="<s:property value="'uploadFiles/'+myUploadFileName"/>"><br/>
</body>
</html>