2013-02-11 81 views
0

在我的Java應用程序存儲圖像放入文件夾我做..將圖像存儲到應用程序根目錄中使用Java?

@RemotingInclude 
public void uploadImage(byte[] content,String fileName) { 
    try 
    { 
     fileName = System.getProperty("java.io.tmpdir") + "/" + fileName; 
     File f = new File(fileName); 
     FileOutputStream fos = new FileOutputStream(f); 
     fos.write(content); 
     fos.close(); 
     System.out.println(f+" "+fileName); 
    }catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 

} 

它存儲一些臨時目錄...但我想保存圖像到文件夾下的應用程序根目錄,因爲如果它在運行上僅在根目錄中存儲的僅服務器映像。

如何將映像存儲到臨時目錄中的文件夾?

回答

0

試試這個:

ServletContext context = getContext(); 
fileName = context.getRealPath("/<yourfoldername>/" + fileName); 
+0

'的getContext()'是一個存根方法來訪問你的servlet上下文。 – ogzd 2013-02-11 08:25:30

+0

這是不是Servlet ..只是它是Flex +正常的Java程序,所以只是它的示例方法.. – 2013-02-11 08:30:40

+1

你試過'新的文件(「./文件夾/文件名」)? – ogzd 2013-02-11 08:33:29

相關問題