简体   繁体   English

如何在tomcat文件夹中写入文件?

[英]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.

相关问题 如何使用Java在tomcat webapps文件夹之外读写图像 - How can I read and write Image outside tomcat webapps folder using java 如何将对象写入原始文件夹文件 - How can I write an Object to my raw folder file 如何将jar文件添加到项目中,而不是添加到Tomcat lib文件夹中? - How can I add jar file to project instead of adding to Tomcat lib folder? 经过xslt转换后,为什么在Tomcat / bin文件夹中生成文件? 如何将此目录更改为webapps / myApp? - After xslt transformation why file is generated in Tomcat/bin folder ? How can i change this directory to webapps/myApp? 如何在Tomcat 8中创建新的共享文件夹并从中加载我的属性文件 - How can I create a new shared folder in Tomcat 8 and load my properties file from that 如何使用maven将文件复制到tomcat webapp文件夹? - How do I copy a file to tomcat webapp folder using maven? 如何在服务器(Tomcat 7)中创建文件夹? - How can I create a folder inside a server (Tomcat 7)? 如何在tomcat上列出文件夹中的所有文件? - How can I list all the files in folder on tomcat? 可以从Tomcat Temp文件夹中读取文件 - Can a read a file from Tomcat Temp folder 我的Tomcat应用程序可以在Ubuntu服务器上写什么文件夹 - What Folder(s) Can My Tomcat Application Write To on Ubuntu Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM