根据慕课网的Socket通信实例课程,完成的文件上传器实例。某些小问题导致我卡了很久
1.未对实体类进行序列化
2.数据库blob类型反向工程在java中实体类对应字段生成String类型,由于理解有问题,本想将图片信息的字节信息转成字符串信息,但是无法保存图片信息,怎么保存都是11个字节。现在还是不知道BufferedInputStream的read(buff)方法,buff里面存的11个字节的信息代表了什么,感觉像是hashcode地址。
后来直接将对象字段改成byte[]类型,BufferedInputStream的read(buff)方法读取存入buff数组,然后包装成file类型,终于能成功保存到数据库了。
主要是按照老师的思路来,然后自己加了一个Hibernate框架,感觉Hibernate框架不熟练对我来说也是一个坑。
实体类:
文件实体类TbFfile:
package com.imooc.entity;
import com.mysql.jdbc.Blob;
/**
* TbFile entity. @author MyEclipse Persistence Tools
*/
public class TbFile implements java.io.Serializable {
// Fields
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer fid;
private String fname;
private byte[] fcontent;
// Constructors
/** default constructor */
public TbFile() {
}
/** minimal constructor */
public TbFile(Integer fid, String fname) {
this.fid = fid;
this.fname = fname;
}
public TbFile(String fname, byte[] fcontent) {
super();
this.fname = fname;
this.fcontent = fcontent;
}
/** full constructor */
public TbFile(Integer fid, String fname, byte[] fcontent) {
this.fid = fid;
this.fname = fname;
this.fcontent = fcontent;
}
// Property accessors
public Integer getFid() {
return this.fid;
}
public void setFid(Integer fid) {
this.fid = fid;
}
public String getFname() {
return this.fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public byte[] getFcontent() {
return this.fcontent;
}
public void setFcontent(byte[] fcontent) {
this.fcontent = fcontent;
}
}
用户实体类 TbUser:
package com.imooc.entity;
/**
* TbUser entity. @author MyEclipse Persistence Tools
*/
public class TbUser implements java.io.Serializable {
// Fields
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String username;
private String password;
// Constructors
/** default constructor */
public TbUser() {
}
public TbUser(String username, String password) {
this.username = username;
this.password = password;
}
/** full constructor */
public TbUser(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
}
业务类(主要实现用户类和文件类和数据库的交互):
文件业务操作FileService:
package com.imooc.service;
import java.io.File;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.imooc.entity.TbFile;
import com.imooc.util.HibernateSessionFactory;
/*
* 文件业务处理