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>