2012-01-19 19 views
5

私はフォルダ構造を以下している:同じフォルダにSpring MVC:コントローラへのファイルパスを取得する方法は?

ProjectFolder/images/some images 

ProjectFolder/WEB-INF/classes/com/xyz/here is java file of controller. 

は、どのように私は、コントローラ内の画像パスを得ることができますか?

お願いします。 感謝:)

+0

なぜあなたは単に使用することはできません '/イメージ/いくつかのイメージ '? – adarshr

+0

同じ試みをしましたが、FileNotFound例外が発生しました。 –

+0

あなたはそのコードを私たちに見せてもらえますか? – adarshr

答えて

9

Webコンテキストは次のようなものが

InputStream is = null ; 
is = request.getSession().getServletContext().getResourceAsStream("/images/someimage.jpg"); 

を助けることができるか、このようなものかもしれかもしれその場合:

InputStream is = null ; 
String realPath = request.getSession().getServletContext().getRealPath("/images/someimage.jpg"); 
is = new FileInputStream(realPath); 
0

あなたはプロパティファイルであなたのイメージのパスを格納することができます。

クラスパスにそのプロパティファイルを格納します。

は今、このようなあなたのコントローラクラスで、そのプロパティにアクセスします。あなたのようなあなたのプロパティファイルでエスケープcharaterを使用する必要があります

Properties properties = new Properties(); 
/*to access your filename.properties file */ 
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("filename.properties")); 

String sServerLocation = properties.getProperty("server.upload.docs.path"); 

注:

server.upload.docs.path=D:\\JDIS3\\DOCS\\ 
関連する問題