简体   繁体   English

如何使用URI创建java.io.File对象?

[英]How to create a java.io.File Object with URI?

Users can upload images on my website. 用户可以在我的网站上传图像。 My servlet needs to write the uploads into directory images of my webapp. 我的servlet需要将上传内容写入我的webapp的目录映像中。 For writing it onto the disk I need to create a File() object. 要将其写入磁盘,我需要创建一个File()对象。

One way is that I can hardcode the complete path to something like 一种方法是我可以将完整路径硬编码为类似的东西

new File("/usr/tomcat/webapps/ROOT/images/file.jpg")

Is there a way so that I can specify something like new File("images/file.jpg") .. Can I do it using new File (URI uri ) so that I do not have to hardcode the complete path. 有没有办法让我可以指定像new File("images/file.jpg")这样的东西..我可以使用新文件(URI uri)来做到这一点,这样我就不必硬编码完整的路径了。 My servlet is located inside /usr/tomcat/webapps/ROOT/WEB-INF/classes/ 我的servlet位于/usr/tomcat/webapps/ROOT/WEB-INF/classes/

The images folder is not a directory. images文件夹不是目录。 It is actually a symbolic link which is pointing to another directory on the filesystem outside tomcat 它实际上是一个符号链接,它指向tomcat外部文件系统上的另一个目录

Don't store images under your webapp deployment directory: next time you'll redeploy the app, you will lose all your images. 不要将图像存储在您的webapp部署目录下:下次重新部署应用程序时,您将丢失所有图像。 And moreover, there is not always a directory for a webapp, since it's typically deployed as a war file. 此外,并不总是有一个webapp目录,因为它通常部署为war文件。

Store your images in an external location, at an absolute path, and configure Tomcat or another web server to serve images from this location, or implement a servlet which serves images from this location. 将图像存储在外部位置,绝对路径,并配置Tomcat或其他Web服务器以从此位置提供图像,或实现从此位置提供图像的servlet。

See Image Upload and Display in JSP for additional pointers. 有关其他指示,请参阅JSP中的图像上载和显示

EDIT: 编辑:

If your problem is the hard-coded absolute path in the Java code, use a System property that you set when starting Tomcat, or a context param in the web.xml, or use a properties file to store the path, or your database. 如果您的问题是Java代码中的硬编码绝对路径,请使用您在启动Tomcat时设置的System属性,或web.xml中的上下文参数,或使用属性文件来存储路径或数据库。

You can get the web app path from the ServletContext object. 您可以从ServletContext对象获取Web应用程序路径。 You get the ServletContext from the HttpServletRequest object ( http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getServletContext%28%29 ). 您从HttpServletRequest对象获取ServletContext( http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getServletContext%28%29 )。
See http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getRealPath%28java.lang.String%29 请参阅http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getRealPath%28java.lang.String%29
Concatenate the "real path" to the desired image file path and provide this as the parameter to the File constructor. 将“真实路径”连接到所需的图像文件路径,并将其作为File构造函数的参数提供。

 try{    

String directory = //Store it in your Property file and pick it 

String filename= //Generate Dynamic name or any specific format 
   // Create file    
  FileWriter fstream = new FileWriter(directory+filename);   
  BufferedWriter out = new BufferedWriter(fstream);  
    out.write("your content"); }
catch (Exception e){   
  System.err.println("Error: " + e.getMessage()); }
finally {  
   //Close the output stream
     out.close();
 } 

You Can keep the directory path in you property file if it is not changing frequently. 如果不经常更改,您可以将目录路径保留在属性文件中。 Directory name can be absolute path which may be a hardrive path or you can save it in your webapp also but it always advisable any input file keep it outside the webapp. 目录名称可以是绝对路径,也可以是硬盘路径,也可以将其保存在webapp中,但始终建议将任何输入文件保留在webapp之外。

File naming is always up to you if you want to keep the same file name then need to handle duplicate check other wise specify some dynamic format and named it. 如果你想保持相同的文件名然后需要处理重复检查,文件命名总是由你决定,否则指定一些动态格式并命名它。

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

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