struts2上传文件的小例子

时间:2022-08-28 19:06:18

准备工作:看看lib文件夹下有没有commons-io和commons-fileupload这两个jar文件

如图struts2上传文件的小例子


之后呢,我们需要两个jsp文件一个用来写上传表单,一个用来显示刚刚上传的文件,我待会会上传一个图片,所以在这里的显示结果页面我就用了img

上传页面的jsp内容

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<s:form action="upload" enctype="multipart/form-data">
			<s:file name="upload" label="选择文件"/>
			<s:submit/>
		</s:form>
	</body>
</html>

结果显示页的内容(我自己加了一些显示参数的东东)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		上传成功
		savePath = <s:property value="realPath"/>
		<hr>
		uploadFileName = <s:property value="uploadFileName"/>
		<hr>
		uploadContentType = <s:property value="uploadContentType"/>
		<hr>
		<img alt="上传的图片" src='<s:property value="realPath"/>'>
	</body>
</html>

struts.xml的配置

 <span style="white-space:pre">	</span><action name="upload" class="action.TestUpload">
        	<param name="savePath">/uploadFiles</param>
        	<result>/content/succ.jsp</result>
        </action>
savePath就是上传的文件的存放路径

接下来就是action类的内容


此处的uploadContentType和uploadFileName如果没有也能用(使用getUpload().getName())即可获得文件名,但是这样弄的文件名就不是原来的文件名了


package action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class TestUpload extends ActionSupport {
	private File upload;
	private String uploadContentType;
	private String uploadFileName;
	private String savePath;
	private String realPath;
	
	public String getRealPath() {
		return realPath;
	}
	public void setRealPath(String realPath) {
		this.realPath = realPath;
	}
	public void setSavePath(String value) {
		this.savePath = value;
	}
	private String getSavePath() throws Exception {
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}
	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 execute() throws Exception {
		//获取上传的文件路径
		setRealPath(getSavePath() + "\\" + getUploadFileName());
		//创建一个输出流,准备用来在上传文件夹中写入文件内容
		FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getUploadFileName());
		//创建一个输入流,获取刚刚上传的文件
		FileInputStream fis = new FileInputStream(getUpload());
		//相当于一次写入1kb的文件内容
		byte[] buffer = new byte[1024];
		int len = 0;
		//从输入流中读取字节,len是用来计算本次循环所读取的长度
		while((len = fis.read(buffer)) > 0) {
			//写入输出流
			fos.write(buffer, 0, len);
		}
		return SUCCESS;
	}
}