存储文件在Spring Boot部署后的资源文件夹中。

huangapple go评论77阅读模式
英文:

store file in spring boot resource folder after deployment

问题

I have deployed a spring-boot application JAR file. Now, I want to upload the image from android and store it in the myfolder of resource directory. But unable to get the path of resource directory.

Error is:
java.io.FileNotFoundException: src/main/resources/static/myfolder/myimage.png
(No such file or directory)

This is the code for storing the file in the resource folder

private final String RESOURCE_PATH = "src/main/resources";
String filepath = "/myfolder/";

public String saveFile(byte[] bytes, String filepath, String filename) throws MalformedURLException, IOException {

    File file = new File(RESOURCE_PATH + filepath + filename);
        
    OutputStream out = new FileOutputStream(file);
    try {
        out.write(bytes);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        out.close();
    }
    return file.getName();
}

UPDATED:

This is what I have tried

private final String RESOURCE_PATH = "config/";
controller class: 

String filepath = "myfolder/";
String filename = "newfile.png"

public String saveFile(byte[] bytes, String filepath, String filename) throws MalformedURLException, IOException {
    
    //reading old file        
    System.out.println(Files.readAllBytes(Paths.get("config","myfolder","oldfile.png")));  //gives noSuchFileException
    
    //writing new file
    File file = new File(RESOURCE_PATH + filepath + filename);

    OutputStream out = new FileOutputStream(file);    //FileNotFoundException
    try {
        out.write(bytes);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        out.close();
    }
    return file.getName();
}

Project structure:

+springdemo-0.0.1-application.zip
+config
+myfolder
-oldfile.png
-application.properties
+lib
+springdemo-0.0.1.jar
+start.sh
-springdemo-0.0.1.jar //running this jar file

英文:

I have deployed a spring-boot application JAR file. Now, I want to upload the image from android and store it in the myfolder of resource directory. But unable to get the path of resource directory.

Error is:
java.io.FileNotFoundException: src/main/resources/static/myfolder/myimage.png
(No such file or directory)

This is the code for storing the file in the resource folder

private final String RESOURCE_PATH = "src/main/resources";
String filepath = "/myfolder/";

public String saveFile(byte[] bytes, String filepath, String filename) throws MalformedURLException, IOException {

File file = new File(RESOURCE_PATH + filepath + filename);
	
OutputStream out = new FileOutputStream(file);
try {
	out.write(bytes);
} catch (Exception e) {
	e.printStackTrace();
} finally {
	out.close();
}
return file.getName();

}

UPDATED:

This is what I have tried

private final String RESOURCE_PATH = "config/";
controller class: 

String filepath = "myfolder/";
String filename = "newfile.png"

public String saveFile(byte[] bytes, String filepath, String filename) throws MalformedURLException, IOException {
	
	//reading old file		
	System.out.println(Files.readAllBytes(Paths.get("config","myfolder","oldfile.png")));  //gives noSuchFileException
	
	//writing new file
	File file = new File(RESOURCE_PATH + filepath + filename);

	OutputStream out = new FileOutputStream(file);	//FileNotFoundException
	try {
		out.write(bytes);
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		out.close();
	}
	return file.getName();
}

Project structure:

+springdemo-0.0.1-application.zip
   +config
     +myfolder
        -oldfile.png
     -application.properties
   +lib
     +springdemo-0.0.1.jar
   +start.sh
-springdemo-0.0.1.jar		//running this jar file

答案1

得分: 5

通常,在部署应用程序(或使用Java启动应用程序)时,您会启动一个JAR文件。您并没有资源文件夹。您可以创建一个并访问它,但当应用程序运行时(假设您试图以独立方式运行它),它肯定不会是 src/main/resources

当构建最终的构建成果(您的应用程序)时,它会创建一个JAR(或EAR或WAR)文件,并将您在 src/main/resources 文件夹中的资源复制到输出目录并包含在最终构建成果中。在应用程序运行时,该文件夹根本就不存在。

在构建过程中,target/ 被创建,并包含类、资源、测试资源等(假设您使用Maven构建;如果您使用Gradle、Ant或手动构建,则会有些不同)。

您可以创建一个名为 docs 的文件夹,放在最终构建成果旁边,为它设置适当的权限(chmod/chown),并让您的应用程序输出文件到该文件夹中。这个文件夹也应该存在于运行您的构建成果的目标机器上,所以如果不存在,可能是文件夹不存在或应用程序缺少从该文件夹读取/写入的适当权限。

如果您需要更多细节,请不要犹豫提问。

更新:
要访问一个作为资源捆绑在您的构建成果中(例如 final.jar)的资源,您应该能够使用以下方式检索它:

testText = new String(ControllerClass.class.getResourceAsStream("/test.txt").readAllBytes());

这假设您的 test.txt 文件位于 src/main/resources 目录的直接下方,并且已捆绑到您的JAR文件的根目录(或从您的应用程序运行的目标文件夹)。ControllerClass 是访问文件的控制器。readAllBytes 正好是做这个事情:读取文本文件的所有字节。要访问构建成果内的图像,您可能想要使用 ImageIO

但是,如果您想要访问一个不是捆绑在构建成果内的外部文件,您可以使用 File image = new File(...),其中 ... 可能是类似于 "docs/image.png" 的内容。这将需要您在JAR构建成果旁边创建一个名为 docs 的文件夹,并在其中放置一个名为 image.png 的文件。

当然,您也可以使用流进行操作,有各种有用的库可用于处理输入和输出流。

以下内容是针对AWT的,但如果您真的希望访问图像的字节,它也适用:ImageIO。通常,在控制器中,您不会想这样做,而是让用户从一个可用的文件夹中访问(并因此下载)它。

希望这对您有所帮助 :).

英文:

Usually when you deploy an application (or start it using Java), you start a JAR file. You don't have a resource folder. You can have one and access it, too, but it certainly won't be src/main/resources.

When you build your final artifact (your application), it creates a JAR (or EAR or WAR) file and your resources, which you had in your src/main/resources-folder, are copied over to the output directory and included in the final artifact. That folder simply does not exist when the application is run (assuming you are trying to run it standalone).

During the build process target/ is created and contains the classes, resources, test-resources and the likes (assuming you are building with Maven; it is a little different if you build using Gradle or Ant or by hand).

What you can do is create a folder e.g. docs next to your final artifact, give it the appropriate permissions (chmod/chown) and have your application output files into that folder. This folder is then expected to exist on the target machine running your artifact, too, so if it doesn't, it would mean the folder does not exist or the application lacks the proper permissions to read from / write to that folder.

If you need more details, don't hesitate to ask.

Update:
To access a resource, which is bundled and hence inside your artifact (e.g. final.jar), you should be able to retrieve it by using e.g. the following:

testText = new String(ControllerClass.class.getResourceAsStream("/test.txt").readAllBytes());

This is assuming your test.txt file is right under src/main/resources and was bundled to be directly in the root of your JAR-file (or target folder where your application is run from). ControllerClass is the controller, which is accessing the file. readAllBytes just does exactly this: read all the bytes from a text file. For accessing images inside your artifact, you might want to use ImageIO.

IF you however want to access an external file, which is not bundled and hence not inside your artifact, you may use File image = new File(...) where ... would be something like "docs/image.png". This would require you to create a folder called docs next to your JAR-artifact and put a file image.png inside of it.

You of course also may work with streams and there are various helpful libraries for working with input- and output streams.

The following was meant for AWT, but it works in case you really want to access the bytes of your image: ImageIO. In a controller you usually wouldn't want to do that, but rather have your users access (and thus download) it from a given available folder.

I hope this helps :).

huangapple
  • 本文由 发表于 2020年8月11日 17:05:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63355006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定