Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.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中保存inputstream中的缩略图_Java - Fatal编程技术网

如何在java中保存inputstream中的缩略图

如何在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);

我正在将inputstream从android应用程序发送到我的java Web服务,并使用以下代码保存在我的Web服务器上:

            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;我必须把输入流放进去,因为这就是我得到的,你必须展示你是如何读取输入流的-我在上面的代码中没有看到它-你说你把它保存在一个文件中-然后读取该文件-并创建缩略图