Spring Boot |将映像上载到资源中的相对路径

Spring Boot |将映像上载到资源中的相对路径,spring,file-upload,spring-boot,image-uploading,relative-path,Spring,File Upload,Spring Boot,Image Uploading,Relative Path,当我尝试将图像上载到资源中的项目文件夹时,会出现“系统找不到指定的路径”。 以下是我的项目结构: |计划 |src |主要 |资源 |META-INF.resources |图像 我已将路径定义为 String path = "\\resources\\images\\" + imageName; File file = new File(path ); try { InputStream is = event.getFile().getInputstream();

当我尝试将图像上载到资源中的项目文件夹时,会出现“系统找不到指定的路径”。 以下是我的项目结构:

|计划 |src |主要 |资源 |META-INF.resources |图像

我已将路径定义为

String path = "\\resources\\images\\" + imageName; File file = new File(path );
    try {
        InputStream is = event.getFile().getInputstream();
        OutputStream out = new FileOutputStream(path );
        byte buf[] = new byte[1024];
        int len;
        while ((len = is.read(buf)) > 0)
            out.write(buf, 0, len);
        is.close();
        out.close();
    } catch (Exception e) {
        System.out.println(e);
    }

META-INF.resources目录下的images文件夹的确切路径是什么?

以下操作应该可以正常工作:

//App.java 
    String path = "\\META-INF.resources\\images\\" + imageName;
    InputStream is = App.class.getClassLoader().getResourceAsStream(

                    path);

在shell中,使用“getClassLoader().getResourceAsStream()”而不是FileInputStream或FileOutputStream。

以下操作应该可以正常工作:

//App.java 
    String path = "\\META-INF.resources\\images\\" + imageName;
    InputStream is = App.class.getClassLoader().getResourceAsStream(

                    path);
在一个简单的例子中,使用“getClassLoader().getResourceAsStream()”而不是FileInputStream或FileOutputStream。

下面的方法对我很有效。 在application.properties中,我将路径字符串定义为:

image.path=src/main/resources/META-INF/resources/images/uploads/

这是我在类文件中的uploadImage函数

 @Autowired
private Environment environment;

public String uploadImg(FileUploadEvent event, String imagePrefix) {

    String path = environment.getProperty("image.path");

    SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmss");
    String name = fmt.format(new Date())
            + event.getFile().getFileName().substring(
            event.getFile().getFileName().lastIndexOf('.'));
    name = String.format("%s%s" ,imagePrefix ,name );
    String finalPath = String.format("%s%s" ,path ,name);
    System.out.println("image is going to save @ " +finalPath);

    File file = new File(finalPath).getAbsoluteFile();

    try {
        InputStream is = event.getFile().getInputstream();
        OutputStream out = new FileOutputStream(file);
        byte buf[] = new byte[1024];
        int len;
        while ((len = is.read(buf)) > 0)
            out.write(buf, 0, len);
        is.close();
        out.close();
    } catch (Exception e) {
        System.out.println(e);
    }
    return name;
}
我不确定它是否能在生产中发挥作用。基本上,我得到项目的绝对路径,然后附加相对提取的路径。如果我错了,请纠正我。

以下内容对我很有用。 在application.properties中,我将路径字符串定义为:

image.path=src/main/resources/META-INF/resources/images/uploads/

这是我在类文件中的uploadImage函数

 @Autowired
private Environment environment;

public String uploadImg(FileUploadEvent event, String imagePrefix) {

    String path = environment.getProperty("image.path");

    SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmss");
    String name = fmt.format(new Date())
            + event.getFile().getFileName().substring(
            event.getFile().getFileName().lastIndexOf('.'));
    name = String.format("%s%s" ,imagePrefix ,name );
    String finalPath = String.format("%s%s" ,path ,name);
    System.out.println("image is going to save @ " +finalPath);

    File file = new File(finalPath).getAbsoluteFile();

    try {
        InputStream is = event.getFile().getInputstream();
        OutputStream out = new FileOutputStream(file);
        byte buf[] = new byte[1024];
        int len;
        while ((len = is.read(buf)) > 0)
            out.write(buf, 0, len);
        is.close();
        out.close();
    } catch (Exception e) {
        System.out.println(e);
    }
    return name;
}

我不确定它是否能在生产中发挥作用。基本上,我得到项目的绝对路径,然后附加相对提取的路径。如果我错了,请纠正我。

\META-INF.resources\images\实际上对我的案例不起作用。这不是一个maven项目吗?是的。这是一个专业项目。但我仍然面临“系统无法找到指定的路径”。是的,您的权利我可以复制您的问题,我已经更改了我的答案,请尝试。感谢@Shaik Elias的帮助。它是intelliJ,将项目目录显示为META-INF.resources作为单个目录名。但当我通过explorer进入项目结构时,META-INF是一个单独的文件夹,resources是它的子文件夹。您以前的建议对我的案例有效。\META-INF.resources\images\实际上对我的案例无效。这不是一个maven项目吗?是的。这是一个专业项目。但我仍然面临“系统无法找到指定的路径”。是的,您的权利我可以复制您的问题,我已经更改了我的答案,请尝试。感谢@Shaik Elias的帮助。它是intelliJ,将项目目录显示为META-INF.resources作为单个目录名。但当我通过explorer进入项目结构时,META-INF是一个单独的文件夹,resources是它的子文件夹。您之前的建议对我的案例有效。您是否尝试了`ServletContext`?
String path=context.getRealPath(“资源/图像”+imageName)
应该在您获得
ServletContext
后工作。您是否尝试了`ServletContext`?
String path=context.getRealPath(“资源/图像”+imageName)应该在获得
ServletContext