우분투에서 응용 프로그램을 개발 중입니다. 한 Java 웹 스프링 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;
}
하지만 난 바람둥이 서버에 배포하고 실행할 때, 파일이 루트에 만들어지고 있습니다.
I가 fileLoc의 값을 출력
, 그것은/root/myapp/file-uploads/01_16_2014_000924_document.jpg
는 I 컨트롤러에 메인 수단을 추가 나타낸다. 톰캣에서 실행 때 루트를주고 왜 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);
}
내가 자바 응용 프로그램으로 이것을 실행 , 나는 적절한 출력
Home Path : /home/shiju
Separator :/
는 무엇입니까?
어떤 사용자가 tomcat 프로세스를 실행하고 있습니까? – MadProgrammer
Tomcat을 루트로 실행 하시겠습니까? –
Tomcat이 루트로 실행 중이므로 오류가 발생 했습니까? – John3136