java 读取和保存图片

// 读取图片文件
InputStream in = new FileInputStream( "c:/test.png");
byte[] photo = new byte[in.available()];
in.read(photo);
in.close();


OutputStream out = new FileOutputStream("c:/copy.png");
out.write(user.getPhoto());
out.close();

使用到的实体User
/**
* 实体
*
* @author tyg
*
*/
public class User {
private int id;
private String name;
private Integer age;
private Date birthday; // 生日
private String desc; // 一大段说明
private byte[] photo; // 头像图片

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public Date getBirthday() {
return birthday;
}

public void setBirthday(Date birthday) {
this.birthday = birthday;
}

public String getDesc() {
return desc;
}

public void setDesc(String desc) {
this.desc = desc;
}

public byte[] getPhoto() {
return photo;
}

public void setPhoto(byte[] photo) {
this.photo = photo;
}

public void setAge(Integer age) {
this.age = age;
}

@Override
public String toString() {
return "[User: id=" + id + ", name=" + name + "]";
}

}

你可能感兴趣的:(java基础)