Ubuntu でアプリケーションを開発しています。Java Web Spring MVC アプリケーションが 1 つあります。その中で私はコントローラーを持っています。クライアントはファイルをアップロードできます (AngularJS を介した投稿)。コントローラーでは、ファイルを取得して特定の場所にコピーしています。
これが私のコントローラーです
@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
コントローラーにmainメソッドを追加しました。
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 で実行しているときに root を与えるのはなぜですか?