2015-08-26 91 views
0

如何將文件保存到項目外部的相對路徑?所以我可以在程序運行的任何計算機上創建一個資源文件夾?在項目外的相對路徑中創建文件夾

我想:

String folderPath=getClass().getClassLoader().getResource(".").getPath()+other stuff 

要創造一個我保存圖像的文件夾路徑。它創建了一個像tomcat%20v7.0這樣的文件夾,確實保存了我的圖片。我保存數據庫中每張圖片的絕對路徑,然後將它們加載到一個jsp文件中。在eclipse中運行應用程序時,一切正常。試圖在瀏覽器中運行時,不會顯示照片。 瀏覽器安裝在C:和tomcat /月食E:

+0

首先你需要知道該服務器可以在HTTP protocal的,我不會看你的c:/ yourfile路徑....你需要在tomcat或您的服務器的webapp文件夾中保存文件........ – koutuk

+0

以及如何獲取我的服務器webapp的路徑? – dskfdskjgds

+0

是他們在我發佈的答案中的任何問題 – koutuk

回答

-1
public static List<String> getEndPoints() throws   
       MalformedObjectNameException, 
      NullPointerException, UnknownHostException, 
      AttributeNotFoundException, InstanceNotFoundException, 
      MBeanException, ReflectionException { 


     MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 
     Set<ObjectName> objs = mbs.queryNames(new ObjectName(
       "*:type=Connector,*"), Query.match(Query.attr("protocol"), 
       Query.value("HTTP/1.1"))); 

     String hostname = InetAddress.getLocalHost().getHostName(); 
     InetAddress[] addresses = InetAddress.getAllByName(hostname); 
     ArrayList<String> endPoints = new ArrayList<String>(); 

     for (Iterator<ObjectName> i = objs.iterator(); i.hasNext();) { 
      ObjectName obj = i.next(); 
      String scheme = mbs.getAttribute(obj, "scheme").toString(); 
      String port = obj.getKeyProperty("port"); 
      for (InetAddress addr : addresses) { 
       String host = addr.getHostAddress(); 
       String ep = scheme + "://" + host + ":" + port; 
       endPoints.add(ep); 
      } 
     } 
     return endPoints; 
    } 

//use above code to get path like "http://yourip:yourport/yourwebapppath" 
相關問題