2014-01-16 37 views
0

我正在Ubuntu中開發我的應用程序。我有一個Java Web Spring MVC應用程序。我有一個控制器。客戶端可以上傳文件(通過AngularJS發佈)。在控制器中,我正在獲取文件並將其複製到特定位置。在Tomcat上運行時,System.getProperty(「user.home」)返回/ root

這裏是我的控制器

@RequestMapping(value = "/fileUpload", method = RequestMethod.POST) 
@ResponseBody 
public String UploadFile(HttpServletRequest request,HttpServletResponse response) { 

    SimpleDateFormat sdf = new SimpleDateFormat("MM_dd_yyyy_HHmmss"); 
    String date = sdf.format(new Date()); 

    String fileLoc = null; 

    MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request; 

    Iterator<String> itr = mRequest.getFileNames(); 
    while (itr.hasNext()) { 
     MultipartFile mFile = mRequest.getFile(itr.next()); 
     String fileName = mFile.getOriginalFilename(); 

     String homePath=System.getProperty("user.home"); 
     String separator=File.separator; 

     fileLoc = homePath + separator + "myapp" + separator + "file-uploads" + 
        separator + date + "_" + fileName; 

     System.out.println(fileLoc); 
     try { 
      File file = new File(fileLoc); 

      // If the directory does not exist, create it 
      if (!file.getParentFile().exists()) { 
       file.getParentFile().mkdirs(); 
      } 
      FileCopyUtils.copy(mFile.getBytes(), file); 

     } 
     catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
     catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 
    return fileLoc; 
} 

但是,當我在Tomcat服務器中部署和運行,該文件是越來越根創建的。

當我打印的值fileLoc,它顯示

/root/myapp/file-uploads/01_16_2014_000924_document.jpg 

我在控制器加入方法。

public static void main(String[] args) { 
    String homePath=System.getProperty("user.home"); 
    String separator=File.separator; 

    System.out.println("Home Path: " + homePath); 
    System.out.println("Separator: " + separator); 
} 

當我運行這是Java應用程序,我得到正確的輸出

Home Path : /home/shiju 
Separator :/

爲什麼Tomcat上運行時竟然放棄根源在哪裏?

+0

在什麼用戶的tomcat進程運行? – MadProgrammer

+4

你是否以root身份運行tomcat? –

+0

錯誤,因爲Tomcat作爲root運行? – John3136

回答

5

如果您正在以root用戶身份執行應用程序,那麼顯然/root/將在user.home屬性中返回。

+2

是的。我以root身份運行它。 –

相關問題