1

我正在 Ubuntu 中开发我的应用程序。我有一个 Java Web Spring MVC 应用程序。在那我有一个控制器。客户端可以上传文件(通过 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 服务器中并运行时,该文件是在 root 中创建的。

当我打印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上运行时会出现root?

4

1 回答 1

6

如果您正在使用 root 用户执行应用程序,那么很明显/root/将在user.home属性中返回。

于 2014-01-16T05:25:29.660 回答