日期:2014-05-16 浏览次数:20675 次
CREATE TABLE `users` ( `enable` int(11) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, `account` varchar(255) DEFAULT NULL, `id` int(11) NOT NULL AUTO_INCREMENT, `resume` text, `photo` blob, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8
import java.sql.Blob; import java.sql.Clob; @Entity @Table(name="users") public class User { private Integer id; private String account; private String password; private Integer enable; private String resume; //定义为string,对应数据库中resume(text)字段 private Blob photo; //定义为blob类型,对应数据库中photo(blob)字段 @Id @GeneratedValue public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getEnable() { return enable; } public String getSummary() { return summary; } public void setSummary(String summary) { this.summary = summary; } public Blob getResume() { return resume; } public void setResume(Blob resume) { this.resume = resume; } public void setEnable(Integer enable) { this.enable = enable; } }
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.List; import java.sql.Blob; import org.apache.log4j.Logger; import org.apache.struts2.ServletActionContext; import org.hibernate.Hibernate; import org.springframework.beans.factory.annotation.Autowired; import cn.ibeans.ssh.users.model.User; import cn.ibeans.ssh.users.service.UserManager; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { private static final long serialVersionUID = 3345502231566725471L; private static Logger logger = Logger.getLogger(UserAction.class); private User user; private UserManager userManager; //以下三项是struts处理文件上传的约定风格 private File photo; private String photoFileName; private String photoContentType; public File getPhoto() { return photo; } public void setPhoto(File photo) { this.photo = photo; } public String getPhotoFileName() { return photoFileName; } public void setPhotoFileName(String photoFileName) { this.photoFileName = photoFileName; } public String getPhotoContentType() { return photoContentType; } public void setPhotoContentType(String photoContentType) { this.photoContentType = photoContentType; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Autowired public void setUserManager(UserManager userManager) { this.userManager = userManager; } /** * * @return * @throws Exception */ public String save() throws Exception{ if(null==user) return this.INPUT; logger.debug("开始日志^"); //以用户提交到服务器端的文件实例为实参构造一个输入流,支持对二进制数据的两种存储方式 InputStream is = new FileInputStream(photo); //1、第一种方式,将二进制文件以blob的方式存储到数据库 //将输入流转换为hibernate能识别的blob型数据对象 user.setPhoto(Hibernate.createBlob(is)); userManager.addUser(user); //2、第二种方式,将二进制文件以文件方式存储到服务器硬盘 /* //取得附件在服务