如何在java中保存inputstream中的缩略图
我正在将inputstream从android应用程序发送到我的java Web服务,并使用以下代码保存在我的Web服务器上:如何在java中保存inputstream中的缩略图,java,Java,我正在将inputstream从android应用程序发送到我的java Web服务,并使用以下代码保存在我的Web服务器上: String filePath = dirPath + File.separator + imageName; // save the file to the server try { File newFile = new File(filePath);
String filePath = dirPath + File.separator + imageName;
// save the file to the server
try {
File newFile = new File(filePath);
boolean fileCreated = true;
if (!newFile.exists()) {
fileCreated = newFile.createNewFile();
}
if (fileCreated) {
FileOutputStream outpuStream = new FileOutputStream(newFile);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = file.read(bytes)) != -1) {
outpuStream.write(bytes, 0, read);
}
outpuStream.flush();
outpuStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
但是现在我想保存一个100*100的缩略图像,有人能告诉我如何做到这一点吗。我尝试使用它,它给了我空异常,没有任何错误
ImageIO.read().getScaledInstance(100, 100, BufferedImage.SCALE_SMOOTH);
如果有人可以告诉我不获取空异常的解决方案,或者告诉我其他方法,请不要建议任何库。谢谢,我将非常感谢您的帮助:)您可以按顺序执行以下操作:
BufferedImage bim=null;
try {
bim=ImageIO.read(newFile));
}
catch (Exception ex) { ex.printStackTrace(); }
File outFile of=new File(..../myimage.png");
BufferedImage img=(BufferedImage)bim.getScaledInstance(100, 100, BufferedImage.SCALE_SMOOTH);
try {
ImageIO.write(img, "png", of);
} catch (IOException ex) { ex.printStackTrace(); }
--
可以按如下方式缩放图像:
BufferedImage img=new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
Graphics g=img.getGraphics();
g.drawImage(bim, 0, 0, img.getWidth(), img.getHeight(), null);
因此,用这个替换getScaledInstance。我试过了,它给我一个异常sun.awt.image.ToolkitImage不能转换为java.awt.image.BufferedImageIt在读取ImageIO.read(myinputstream)后返回null;我必须把输入流放进去,因为这就是我得到的,你必须展示你是如何读取输入流的-我在上面的代码中没有看到它-你说你把它保存在一个文件中-然后读取该文件-并创建缩略图