慕课网Socket实例-文件上传

根据慕课网的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;

/*
 * 文件业务处理
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值