图片的存储

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);

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值