阅读前可以先了解通过流的方式传输图片信息;
https://blog.youkuaiyun.com/everything002/article/details/106392551
简单说,计算机的文件都是以二进制的形式保存在电脑上,所以将图片信息转换为二进制就很容易进行操作了。
Client端:
/*根据图片路径(String)转化成byte[]*/
public static byte[] PicToByte(String pic_path){
byte[] pic_data = new byte[0];
File file = new File(pic_path);
try {
BufferedImage bi = ImageIO.read(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "jpg", baos);
pic_data = baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return pic_data;
}
在domain(package)中的Product(class)定义图片为byte[]类型
public class Product implements Serializable {
private int id; //id
private int number; //商品数量
private String name; //名称
private String pinyin; //拼音名称
private double price; //价格
private String unit; //单位
private byte[] picData;
private String oper; //请求的操作
…………
}
接收从server端发送来的数据时,通过以下方法将byte[]转换为pic
(注意:byte[]仅代表图片数据,正常情况下一张图片需要有一个名字,如:苹果;你才能确定这张图片进行下载或其他操作,所以方法至少需要两个参数)
/*byte[]数组转换pic写入本地*/
public static void ByteToImg(byte[] pic_data, String pic_dame){
File file = new File("res/"+pic_dame); //将图片缓存到项目res文件夹路径下
// System.out.println(file);
/*file放在第一个方便判断本地是否有该文件*/
if(file.exists()){ //当文件存在时
return;
}
else { //文件不存在则创建
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(pic_data);
try {
BufferedImage bufferedImage = ImageIO.read(byteArrayInputStream);
ImageIO.write(bufferedImage, "jpg", file); //中间的参数支持:png, jpg, gif
/*这里将图片同一转换成jpg的样子缓存————尝试将图片后缀去掉后不会自动保存成.jpg;所以需要传入名字*/
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
byteArrayInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}/*ByteToImg*/
Server端:
查询所有商品时的操作( product.setPicData(rs.getBytes(“picture”)); 通过byte[]直接获取数据库图片信息)
//查询所有商品
public ArrayList<Product> queryAllPros(){
ArrayList<Product> allPros = new ArrayList<Product>();
Product product = null;
//通过数据库查询信息
conn = JDBCUtil.DbConnect();
String sql = "select id, picture, number, name, pinyin, price, unit from products";
try {
statment = conn.prepareStatement(sql);
rs = statment.executeQuery();
while (rs.next()){ //默认指向第一行
product = new Product();
//将数据填入对象
product.setId(rs.getInt("id"));
product.setNumber(rs.getInt("number"));
product.setName(rs.getString("name"));
product.setPinyin(rs.getString("pinyin"));
product.setPrice(rs.getDouble("price"));
product.setUnit(rs.getString("unit"));
product.setPicData(rs.getBytes("picture")); //数据库blob类型的图片这里通过byte[]存储
allPros.add(product); //将对象添加至列表
}/*while*/
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtil.DbRelease(rs, statment, conn);
}
return allPros;
}/*queryAllPros*/
插入商品时的操作
( 通过setBinaryStream插入图片
void setBinaryStream(int parameterIndex, java.io.InputStream x,
int length) throws SQLException;)
//添加商品
public void addPro(Product product) {
//通过数据库查询信息
conn = JDBCUtil.DbConnect();
String sql = "insert into products(id, picture, number, name, pinyin, price, unit) values(?, ?, ?, ?, ?, ?, ?)";
try {
statment = conn.prepareStatement(sql);
statment.setInt(1, product.getId());
InputStream picInput = new ByteArrayInputStream(product.getPicData());
statment.setBinaryStream(2, picInput, picInput.available()); //picInput.available()获取长度
statment.setInt(3, product.getNumber());
statment.setString(4, product.getName());
statment.setString(5, product.getPinyin());
statment.setDouble(6, product.getPrice());
statment.setString(7, product.getUnit());
statment.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
JDBCUtil.DbRelease(rs, statment, conn);
}
}/*addPro*/