Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中将BuffereImage保存到FastDFS?_Java - Fatal编程技术网

如何在Java中将BuffereImage保存到FastDFS?

如何在Java中将BuffereImage保存到FastDFS?,java,Java,我有一个图像,我用它做了一些事情,最后我得到了一个BuffereImage对象(原始图像的子图像),现在我想将子图像保存到FastDFS而不保存在本地,我该怎么办 我已经将子映像作为文件保存到本地,但我不想这样做,因为这样会造成浪费 String oriPicPathInFastDFS = "http://127.0.0.1/xx/xx/xx/xx"; BufferedImage image = null; try { image = ImageIO.re

我有一个图像,我用它做了一些事情,最后我得到了一个BuffereImage对象(原始图像的子图像),现在我想将子图像保存到FastDFS而不保存在本地,我该怎么办

我已经将子映像作为文件保存到本地,但我不想这样做,因为这样会造成浪费

    String oriPicPathInFastDFS = "http://127.0.0.1/xx/xx/xx/xx";
    BufferedImage image = null;
    try {
        image = ImageIO.read(new URL(oriPicPathInFastDFS));
    } catch (IOException e) {
        e.printStackTrace();
    }

    // do something

    // this is the sub image that I want to save to FastDFS
    BufferedImage subImage = image.getSubimage(5, 5, 5, 5);

    // these code can save the sub image to my local and then upload to fastDFS
    String localPath = "/home/xx/x/xx.jpg";
    File detectionFile = new File(localPath);
    try {
        detectionFile.createNewFile();
        ImageIO.write(subImage, "jpg", detectionFile);
    } catch (IOException e) {
        e.printStackTrace();
    }

    // upload to fast dfs

我想将子映像上载到fastDFS,而不将其保存到本地。

好的,我找到了解决此问题的方法

    // change the BufferedImage to byte[]
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        boolean flag = ImageIO.write(sunImage, "jpg", out);
    } catch (IOException e) {
        e.printStackTrace();
    }
    byte[] byteArray = out.toByteArray();

    // then save the byteArray to fast DFS