2016-09-20 33 views
1

我使用Eclipse + Vaadin框架。如何獲取Vaadin資源路徑的URL?

首先我將圖像保存到WEB-INF文件夾中。 然後我將這個圖像加載到FileResource變量中,創建Image變量,然後將Image變量添加到Layout中。

的頁面URL到圖像資源的樣子:http://servername/sitename/APP/connector/0/16/source/qwerty1.png

如何在格式使用的圖像外部獲取網址?

變量基本路徑返回本地路徑: 「.../WEB-INF/qwerty1.png」

String str = (String) VaadinService.getCurrent().getBaseDirectory(). 
         getAbsolutePath() +"/WEB-INF/qwerty1.png"; 

File temp = new File(str); 
FileOutputStream fos = new FileOutputStream(temp); 
fos.write(os.toByteArray()); 
fos.flush(); 
fos.close(); 

String basepath = VaadinService.getCurrent().getBaseDirectory(). 
        getAbsolutePath() +"/WEB-INF/qwerty1.png"; 
FileResource resource = new FileResource(new File(basepath)); 
Image image2 = new Image("Image from file", resource); 
addComponent(image2); 
+0

您最好使用ThemeResource並將您的圖像放入您的Vaadin主題文件夾'yourproject/src/main/webapp/VAADIN/themes/yourtheme'中。用'new ThemeResource(「qwerty1.png」)引用它;' –

+0

我可以。但問題是我不能得到這個圖像鏈接外部使用,例如在其他網站使用圖像。 – del1980

+0

亞歷杭德羅是對的。 'webapp'文件夾用於靜態資源。這也是您放置HTML或JSP文件的地方。你不需要爲此使用子文件夾VAADIN。 –

回答

4

如果你把文件...src/main/webapp/VAADIN/image.png,應該使用例如localhost:8080/VAADIN/image.png可用。

+0

謝謝!其作品 – del1980