[英]How can I write a file in tomcat folder?
I have deployed my application in Apache Tomcat. 我已经在Apache Tomcat中部署了应用程序。
I have a folder(assets) to save the files. 我有一个文件夹(资产)来保存文件。 So want to write a file inside webapps/assets
所以想在webapps / assets中写一个文件
I tried the following code for that 我为此尝试了以下代码
private String uploadedFiles(MultipartFile files) throws IOException {
String filePath = "../assets/users/image/" + files.getOriginalFilename();
File file = new File(filePath);
byte[] bytes = file.getBytes();
FileOutputStream out = new FileOutputStream(file);
out.write(bytes);
out.close();
But i am getting java.io.FileNotFoundException: ../assets/myfile.jpg (The system cannot find the path specified) 但是我正在获取java.io.FileNotFoundException:../assets/myfile.jpg(系统找不到指定的路径)
How can save this file? 如何保存该文件?
Note: I want this kind of folder structure. 注意:我想要这种文件夹结构。 Since I will save "../assets/myfile.png" in the database to access from the client application deployed in the same server.
由于我将在数据库中保存“ ../assets/myfile.png”,以便从部署在同一服务器上的客户端应用程序进行访问。
You can get the tomcat home path with 您可以使用以下命令获取tomcat的主路径
System.getProperty( "catalina.base" );
You can then add to this your path, in your case /webapps/assets
然后,您可以将路径添加到此路径中,以
/webapps/assets
Hope this helps :) 希望这可以帮助 :)
Maybe I am wrong, but you should give bytes from Multipart files ! 也许我错了,但是您应该从Multipart文件中提供字节! And such of kind
这样的
../assets/users/image/
../assets/users/image/
is wrong. 是错的。 I think, you should start with root folder, or use
Paths.get()
nio. 我认为,您应该从根文件夹开始,或者使用
Paths.get()
nio。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.