struts2文件上传

action类

	private File upload;
	private String uploadFileName;

	public String add() throws Exception {
		// 1.文件保存的路径
		String filePath = ServletActionContext.getServletContext().getRealPath(
				"/files");
		String dir = generateChildPath(filePath);
		// 2.生成带有随机性的文件名
		String fileName = TokenHelper.generateGUID() + "_" + uploadFileName;
		user.setPath(dir);
		user.setFilename(fileName);
		upload.renameTo(new File(filePath + File.separator + dir, fileName));
		// 5.保存用户
		int res = service.saveUser(user);
		if (res > 0) {
			return SUCCESS;
		}
		return null;
	}

	// 生成以yyyy-MM-dd的格式名称文件夹
	private String generateChildPath(String filePath) {
		Date date = new Date();
		DateFormat formt = new SimpleDateFormat("yyyy-MM-dd");
		String dir = formt.format(date);
		File file = new File(filePath, dir);
		if (!file.exists()) {
			file.mkdirs();
		}
		return dir;
	}

User            domain类

package com.itheima.domain;

import java.io.Serializable;

public class User implements Serializable {
	
	private Integer userID;
	private String userName;
	private String logonName;
	private String logonPwd;
	private String gender;
	private String birthday;
	private String education;
	private String telephone;
	private String hobby;
	private String path;
	private String filename;
	private String remark;
	public Integer getUserID() {
		return userID;
	}
	public void setUserID(Integer userID) {
		this.userID = userID;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getLogonName() {
		return logonName;
	}
	public void setLogonName(String logonName) {
		this.logonName = logonName;
	}
	public String getLogonPwd() {
		return logonPwd;
	}
	public void setLogonPwd(String logonPwd) {
		this.logonPwd = logonPwd;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getBirthday() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
	public String getEducation() {
		return education;
	}
	public void setEducation(String education) {
		this.education = education;
	}
	public String getTelephone() {
		return telephone;
	}
	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}
	public String getHobby() {
		return hobby;
	}
	public void setHobby(String hobby) {
		this.hobby = hobby;
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public String getFilename() {
		return filename;
	}
	public void setFilename(String filename) {
		this.filename = filename;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	
	
}

struts.xml

<action name="add" class="com.itheima.web.action.UserAction" method="add">
			<!-- 配置上传文件的扩展名限制 -->
			<interceptor-ref name="defaultStack">
				<param name="fileUpload.allowedExtensions">.doc,.docx,.pdf</param>
			</interceptor-ref>
			<result name="success" type="redirectAction">findAll</result>
			<result name="input">/user/add.jsp</result>
</action>

add.jsp

<s:file name="upload" size="30" value=""></s:file>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值