java Toolkit.getImage() 的正确路径是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7319036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the correct path for Toolkit.getImage()?
提问by Jill
I need to upload an image file and generate a thumbnail for the uploaded file in my JSF webapplication. The original image is stored on the server in /home/myname/tomcat/webapps/uploads
, while the thumbnail is stored in /home/myname/tomcat/webapps/uploads/thumbs
. I'm using the thumbnail generator class I copied from philreeve.com.
我需要上传一个图像文件并在我的 JSF web 应用程序中为上传的文件生成一个缩略图。原始图像存储在服务器上/home/myname/tomcat/webapps/uploads
,而缩略图存储在/home/myname/tomcat/webapps/uploads/thumbs
. 我正在使用从philreeve.com复制的缩略图生成器类。
I have successfully uploaded the file with help from BalusC. But using Toolkit.getImage()
, I can't access the image.
我已经在BalusC 的帮助下成功上传了文件。但是使用Toolkit.getImage()
,我无法访问图像。
I used the uploaded file's absolute path, like so:
我使用了上传文件的绝对路径,如下所示:
inFilename = file.getAbsolutePath();
The relevant code from the thumbnail generator is:
缩略图生成器的相关代码是:
public static String createThumbnail(String inFilename, String outFilename, int largestDimension) {
...
Image inImage = Toolkit.getDefaultToolkit().getImage(inFilename);
if (inImage.getWidth(null) == -1 || inImage.getHeight(null) == -1) {
return "Error loading file: \"" + new File(inFilename).getAbsolutePath() + "\"";
}
...
}
Since I am already using the absolute path, I don't understand why it is not working. I have also used the following values for inFilename
, but I always get the "Error loading file...".
由于我已经在使用绝对路径,我不明白为什么它不起作用。我还为 使用了以下值inFilename
,但我总是收到“错误加载文件...”。
/home/myname/tomcat/webapps/uploads/filename.ext
/uploads/filename.ext
/home/myname/tomcat/webapps/uploads/filename.ext
/uploads/filename.ext
But I did check the directory, and the image isthere. (I uploaded using /home/myname/tomcat/webapps/uploads/filename.ext
, and it works.) What is the correct path for the image in that directory? Thank you.
但是我确实检查了目录,图像就在那里。(我使用 上传/home/myname/tomcat/webapps/uploads/filename.ext
,并且有效。)该目录中图像的正确路径是什么?谢谢你。
Update
更新
I got the code to work by using:
我使用以下代码使代码正常工作:
Image inImage = ImageIO.read(new File(inFilename));
I still don't understand why Toolkit.getImage()
does not work though.
我仍然不明白为什么Toolkit.getImage()
不起作用。
采纳答案by Aaron Digulla
Are you sure it's a JPEG file? Use an image viewer to make sure nothing bad happened to the file during upload (or that it was an image to begin with).
你确定这是一个JPEG文件?使用图像查看器确保文件在上传过程中没有发生任何问题(或者它是一个图像)。
Also, use new File(inFilename).exists()
to make sure the path is correct. I also suggest to print new File(inFilename).getAbsolutePath()
in error messages because relative paths can hurt you.
此外,用于new File(inFilename).exists()
确保路径正确。我还建议打印new File(inFilename).getAbsolutePath()
错误消息,因为相对路径可能会伤害您。
That said, the rest of the code looks correct.
也就是说,其余的代码看起来是正确的。
回答by ceperman
The problem is that Toolkit.getImage()
does not return the image immediately. The issue is well-described in this bug report, a relevant extract of which is here:
问题是Toolkit.getImage()
不会立即返回图像。这个问题在这个错误报告中有很好的描述,相关的摘录在这里:
This is not a bug. The submitter is not properly using the asynchronous Image API correctly. He assumes that getImage loads all of the image's bits into memory. However, it is well documented that the actual loading of bits does not take place until a call to Component.prepareImage or Graphics.drawImage. In addition, these two functions return beforethe Image is fully loaded. Developers are required to install an ImageObserver to listen for notification that the Image has been fully loaded. Once they receive this notification, they can repaint the Image.
这不是错误。提交者未正确使用异步图像 API。他假设 getImage 将所有图像位加载到内存中。但是,据充分证明,在调用 Component.prepareImage 或 Graphics.drawImage 之前,不会发生位的实际加载。此外,这两个函数在Image 完全加载之前返回。开发者需要安装 ImageObserver 来监听 Image 已经完全加载的通知。收到此通知后,他们可以重新绘制图像。
I found that the answer to this questionworks well:
我发现这个问题的答案很有效:
Image image = new ImageIcon(this.getClass().getResource("/images/bell-icon16.png")).getImage();
Image image = new ImageIcon(this.getClass().getResource("/images/bell-icon16.png")).getImage();
回答by Maxim Welikobratov
May be It's access rights problem? Are you sure your script has rights to read or view file?
可能是它的访问权限问题?您确定您的脚本有权读取或查看文件吗?