2015-08-28 46 views
0

我在web應用程序中創建了文件夾(即上傳)。我想在運行時在「uploads」文件夾內創建一個文件夾取決於用戶的用戶名。爲此我寫下面的代碼。此代碼正在創建文件夾和文件,但位置與我的預期不同。文件夾在eclipse home中不在web應用程序中創建

是我收到的位置是在Eclipse中的位置沒有Web應用程序的位置

D:\PAST\RequiredPlugins\JUNO\eclipse\uploads\datto\adhar.PNG 

然後我收到錯誤FileOutStream說:「系統找不到指定的位置。」

public String getFolderName(String folderName, MultipartFile uploadPhoto) 
      throws ShareMeException { 
     File uploadfFile = null; 
     try { 
      File file = new File("uploads\\" + folderName); 

      if (!file.exists()) { 
       file.mkdir(); 
      } 

      uploadfFile = new File(file.getAbsoluteFile() 
        + "\\"+uploadPhoto.getOriginalFilename()); 
      if (uploadfFile.exists()) { 
       throw new ShareMeException(
         "file already exist please rename it"); 
      } else { 
       uploadfFile.createNewFile(); 
       FileOutputStream fout = new FileOutputStream(uploadfFile); 
       fout.write(uploadPhoto.getBytes()); 
       fout.flush(); 
       fout.close(); 
      } 

     } catch (IOException e) { 
      throw new ShareMeException(e.getMessage()); 
     } 

     return uploadfFile.getAbsolutePath(); 
    } 

我想保存在Web應用程序「上傳」文件夾上傳的文件

回答

0

你可以做一些下面線在你的web應用程序: -

String folderPath= request.getServletContext().getRealPath("/"); 
    File file = new File (folderPath+"upload"); 
    file.mkdir(); 
+0

創建的文件夾位於意外路徑「workspace \ .metadata \ .plugins \ org.eclipse.wst.server.core \ tmp1 \ wtpwebapps \ ShareM e \」。 – Musaddique

1

你的文件名不是絕對的:uploads\folderName將針對Eclipse啓動程序設置爲JUNO\eclipse的當前目錄進行解析。

您應該引入一個應用程序變量,如APP_HOME,並針對此變量解析任何數據目錄(包括upload)。

另外,我建議不要說出任何東西(沒有文件也沒有目錄)的文件系統的用戶輸入的輸入後:你問的煩惱(Unicode字符的用戶名),尤其是安全漏洞(甚至在組合與unicode的東西)。如果你真的想使用文件系統,請保持文件名匿名(1.data,2.data,...)並將元數據保存在某個數據庫中。

相關問題