1.更换头像
//里面内容是调用方法将选择的电脑图片存储进字节数组,在转发给服务端,在服务端中进行保存。
void changeimageButtonOnAction(ActionEvent event)throws IOException, ClassNotFoundException, SQLException {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("选择图片文件");
fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter[]{new FileChooser.ExtensionFilter("图片文件", new String[]{"*.png", "*.jpg", "*.jpeg", "*.gif"}), new FileChooser.ExtensionFilter("所有文件", new String[]{"*.*"})});
File selectedFile = fileChooser.showOpenDialog((Window)null);
if (selectedFile != null) {
System.out.println("用户选择了文件: " + selectedFile.getAbsolutePath());
//-----------------------
String imagePath1 = selectedFile.getAbsolutePath(); // 替换为你的图片路径
File imageFile1 = new File(imagePath1);
FileInputStream fis = new FileInputStream(imageFile1);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[524288];
int len;
while ((len = fis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
byte[] imageBytes = bos.toByteArray();
fis.close();
bos.close();
MessaAge age = new MessaAge();
age.setFileBytes(imageBytes);
age.setSrc(imagePath1);
age.setMessageType(MarkTool.file);
ClientConnectServerThread.filetoserver(null,age);
System.out.println("图片正在上传服务端");
//-------------------
File imageFile = new File(selectedFile.getAbsolutePath());
Image imagess = new Image(imageFile.toURI().toString());
this.image.setImage(imagess);
User1 u = new User1();
u=user;
u.setImage(selectedFile.getAbsolutePath());
MessaAge er=new MessaAge();
er.setMessageType(MarkTool.image);
er.setJm("ChangePersonalData");
ClientConnectServerThread.person(u,er);
Alert alert;
alert = new Alert(AlertType.INFORMATION);
alert.setTitle("正确");
alert.setHeaderText((String) null);
alert.setContentText("更改成功");
} else {
System.out.println("用户取消了文件选择");
}
}
2.服务端接收客户端传来的存储图片信息
savePath是我在服务端存储照片的文件夹,imageBytes是照片的字节数组,将照片的路径名称进行uuid处理,使其路径唯一,最好写入图片数据即可。
具体效果如下:
public int getFile(MessaAge age) { //接受文件
String savePath = "E:/java.xmu/qaq 1/src/image/"; // 确保路径末尾有斜杠或使用 File.separator
byte[] imageBytes = age.getFileBytes();
String fileName1 = age.getSrc();
try {
// 生成一个唯一的UUID作为文件名前缀
String uuid1 = uuid.generateUniqueNumber();
// String fileName = uuid1 + fileName1;
String fileName = uuid1 + "_image.jpg";
// 保存图片到服务器端指定目录
File file = new File(savePath + fileName);
FileOutputStream fos = new FileOutputStream(file);
// 写入图片数据
fos.write(imageBytes); // 这里的 imageBytes 是你的图片字节数组
fos.close();
System.out.println("Image saved successfully on server.");
return 1;
} catch (IOException e) {
e.printStackTrace();
return 0;
}
}
3.传输文件,通过User1获取图片在服务端的路径,将其转化为字节数组传给客户端。
public MessaAge setFile(User1 tt) throws IOException { //传输文件
MessaAge age = new MessaAge();
age.setMessageType(MarkTool.other);
String imagePath1 = tt.getImage(); // 替换为你的图片路径
File imageFile1 = new File(imagePath1);
FileInputStream fis = new FileInputStream(imageFile1);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[524288];
int len;
while ((len = fis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
byte[] imageBytes = bos.toByteArray();
fis.close();
bos.close();
age.setFileBytes(imageBytes);
age.setSrc(imagePath1);
return age;
}
4.将图片在客户端上显示
将客户端需要显示图片的地方的字节数组进行赋值。
然后进行加载即可,//为以前用本电脑的路径进行。
//加载头像
image.setPreserveRatio(false); // 不保持宽高比例
image.setFitWidth(120); // 设置ImageView的宽度
image.setFitHeight(120); // 设置ImageView的高度
File imageFile = new File(ren.getImage());
// Image imagess = new Image(imageFile.toURI().toString());
Image imagess = new Image(new ByteArrayInputStream(imageBytes));
image.setImage(imagess);