简体   繁体   English

在Java中的File中指定相对路径

[英]Specifying a relative path in File in Java

I'm uploading images using Spring and Hibernate. 我正在使用Spring和Hibernate上传图像。 I'm saving images on the server as follows. 我将图像保存在服务器上,如下所示。

File savedFile = new File("E:/Project/SpringHibernet/MultiplexTicketBooking/web/images/" + itemName);
item.write(savedFile);

Where itemName is the image file name after parsing the request ( enctype="multipart/form-data" ). 其中itemName是解析请求后的图像文件名( enctype="multipart/form-data" )。 I however need to mention the relative path in the constructor of File . 但是,我需要在File的构造函数中提及相对路径 Something like the one shown below. 如下图所示。

File savedFile = new File("MultiplexTicketBooking/web/images/" + itemName);
item.write(savedFile);

But it doesn't work throwing the FileNotFoundException . 但是抛出FileNotFoundException是行不通的。 Is there a way to specify a relative path with File in Java? 有没有一种方法可以在Java中使用File指定相对路径?

Try printing the working directory from your program. 尝试从程序中打印工作目录。

String curDir = System.getProperty("user.dir");

Gets you that directory. 获取该目录。 Then check if the directories MultiplexTicketBooking/web/images/ exist in that directory. 然后检查目录MultiplexTicketBooking/web/images/在该目录中是否存在。

Can't count the number of times I've been mistaken about my current dir and spent some time looking for a file I wrote to... 无法计算我误认为当前目录并花了一些时间来寻找我写入的文件的次数...

You could get the path of your project using the following - 您可以使用以下命令获取项目的路径-

File file = new File("");
System.out.println("" + file.getAbsolutePath());

So you could have a constants or a properties file where you could define your path which is MultiplexTicketBooking/web/images/ after the relative path. 因此,您可以拥有一个constantsproperties文件,您可以在其中定义相对路径后的路径MultiplexTicketBooking/web/images/

You could append your path with the path you get from file.getAbsolutePath() and that will be the real path of the file. 您可以在路径中附加从file.getAbsolutePath()获得的路径,这将是文件的真实路径。 - file.getAbsolutePath() + MultiplexTicketBooking/web/images/ . file.getAbsolutePath() + MultiplexTicketBooking/web/images/

Make sure the folders after the Project path ie MultiplexTicketBooking/web/images/ exist. 确保项目路径之后的文件夹(即MultiplexTicketBooking/web/images/存在。

It seems the server should offer functionality as might be seen in the methods getContextPath() or getRealPath(String) . 似乎服务器应该提供在getContextPath()getRealPath(String)方法中可能看到的功能。 It would be common to build paths based on those types of server related and reproducible paths. 通常会基于与服务器相关且可复制的那些类型的路径来构建路径。 Do not use something like user.dir which makes almost no sense in a server. 不要使用类似user.dir ,这使得几乎没有任何意义的服务器。

Update 更新资料

ServletContext sc=request.getSession().getServletContext(); 
File savedFile = new File(sc.getRealPath("images")+"\\" + itemName);

Rather than use "\\\\" I'd tend to replace that with the following which will cause the correct file separator to be used for each platform. 与其使用"\\\\"不如将其替换为以下内容,这将导致为每个平台使用正确的文件分隔符。 Retain cross-platform compatibility for when the client decides to swap the MS/ISS based server out for a Linux/Tomcat stack. 当客户端决定将基于MS / ISS的服务器换成Linux / Tomcat堆栈时,保持跨平台兼容性。 ;) ;)

File savedFile = new File(sc.getRealPath("images"), itemName); //note the ','

See File(String,String) for details. 有关详细信息File(String,String)请参见File(String,String)

You can specify the path both absolute and relative with File . 您可以使用File指定绝对路径和相对路径。 The FileNotFoundException can be thrown because the folder might be there. 可能会引发FileNotFoundException因为该文件夹可能在该文件夹中。 Try using the mkdirs() method first in to create the folder structure you need in order to save your file where you're trying to save it. 首先尝试使用mkdirs()方法创建所需的文件夹结构,以便将文件保存在尝试保存的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM