如何在Java中将BuffereImage保存到FastDFS?
我有一个图像,我用它做了一些事情,最后我得到了一个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
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