2015-11-09 53 views
0
的文件夾

嗨,我試圖將上傳的文件保存到我的項目中的文件夾,但我不知道如何給路徑。我還希望上傳文件的同名文件是存儲在文件夾中的文件的名稱。如何將上傳的文件保存到名稱爲

public void handleFileUpload(FileUploadEvent event) throws IOException { 
     LOG.info("Entered into save action event"); 
     FacesMessage message = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded."); 
     FacesContext.getCurrentInstance().addMessage(null, message); 
     String filename = event.getFile().getFileName(); 
     UploadedFile file = event.getFile(); 
     InputStream input = file.getInputstream(); 
     OutputStream output = new FileOutputStream(new File("src/main/resources/uploads/schema.xsd", filename)); 
     try { 
      IOUtils.copy(input, output); 
     } catch (IOException e){ 
      LOG.error("Error in copying file.", e); 
     } 
     finally { 
      IOUtils.closeQuietly(input); 
      IOUtils.closeQuietly(output); 
     } 
    } 

回答

0

你最好的選擇是從服務器的根目錄製作一個絕對路徑的文件夾。這意味着C:如果您使用的是Windows或/如果您使用的是Linux。例如,在Linux服務器,你可以這樣做:

OutputStream output = new FileOutputStream(new File("/var/my_app/uploads/" + filename + "/" + filename + ".xsd")); 
+0

的事情是我可以將它保存在桌面上,但我想它保存到eclps一個floder,這樣我可以在運行時訪問它,我有一個xsd該文件將從相同的位置導入另一個xsd文件。如果我試圖將它保存在桌面上或C:它不是導入。所以我想把xsd寫入一個文件夾。 – kish

+0

你最近怎麼樣?在運行時從classpath加載文件將需要一個自定義類加載器。看看這個:http://stackoverflow.com/questions/60764/how-should-i-load-jars-dynamically-at-runtime – Aurasphere

相關問題