有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为上传的文件创建URL

我正在使用SpringMVC创建一个简单的web应用程序。在这个应用程序中,我为上传文件到服务器(“target”文件夹)编写了代码。文件上传到服务器时没有任何问题。现在我想为上传的文件创建一个URL,可以直接使用URL访问该文件。我该怎么做

这是将文件上载到服务器的代码

@PutMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public boolean saveFile(@RequestPart("file") MultipartFile myFile) {

    System.out.println(myFile.getOriginalFilename());
    try {

        String projectPath = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI()).getAbsolutePath();

        File uploadDir = new File(projectPath + "/uploads");
        uploadDir.mkdir();
        System.out.println(uploadDir.getAbsolutePath());
        myFile.transferTo(new File(uploadDir.getAbsolutePath() + "/" + myFile.getOriginalFilename()));
        System.out.println("File Path " + uploadDir.getAbsolutePath() + "/" + myFile.getOriginalFilename());

        String imgUrl = "../../../../../BackEnd/target/POS-1.0.0/WEB-INF/classes/uploads/" + myFile.getOriginalFilename();

        System.out.println(imgUrl);

        return true;

    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

}

共 (2) 个答案

  1. # 1 楼答案

    您还可以使用内置服务器功能将URL映射到文件系统。取决于您使用的特定应用程序或web服务器

    对于Tomcat,请在服务器中查找此类配置。xml:
    <Context docBase="/my/files/folder" path="/get-file" />

    而且根本不需要编码

  2. # 2 楼答案

    另一种选择是使用Spring Content。与其他解决方案相比,它有几个优点;它为您提供控制器和服务实现,因此您不必自己实现它们。提供对存储的抽象,允许您在不更改代码的情况下重构解决方案。例如,您可以将解决方案移动到云端,并使用类似S3的存储。它可以与其他项目Spring Data(用于捕获有关内容的附加元数据)和ApacheSolr(用于在文档内部搜索)结合使用,总体上会为您提供更丰富的应用程序

    添加到应用程序中非常容易

    pom.xml

        <!  Java API  >
        <!  just change this depdendency if you want to store somewhere else  >
        <dependency>
            <groupId>com.github.paulcwarren</groupId>
            <artifactId>spring-content-fs</artifactId>
            <version>0.6.0</version>
        </dependency>
        <!  REST API  >
        <dependency>
            <groupId>com.github.paulcwarren</groupId>
            <artifactId>spring-content-rest</artifactId>
            <version>0.6.0</version>
        </dependency>
    

    StoreConfig.java

    @Configuration
    // just change this annotation if you want to store somewhere else
    @EnableFilesystemStores
    @Import(RestConfiguration.class)
    public class EnableFilesystemStoresConfig {
    
        @Bean
        File filesystemRoot() {
            try {
                return new File("/path/to/your/files");
            } catch (IOException ioe) {}
            return null;
        }
    
        @Bean
        FileSystemResourceLoader fileSystemResourceLoader() {
            return new FileSystemResourceLoader(filesystemRoot().getAbsolutePath());
        }
    
    }
    

    注:如果你使用的是弹簧靴,那就更容易了

    FileStore.java

      @StoreRestResource(path="files")
      public interface FileStore extends Store<String> {
      }
    

    就这样。文件存储本质上是一个通用的Spring ResourceLoader。spring-content-fs依赖关系将导致Spring内容注入基于文件系统的实现,因此您无需担心自己实现它。此外,如果将HTTP请求转发到{}的方法上的{},那么{}依赖关系将导致Spring内容也注入一个实现

    因此,您现在将在/files拥有一个功能齐全的(POST、PUT、GET、DELETE)基于REST的文件服务,它将使用FileStore在服务器上的/path/to/your/files中检索(和存储)文件

    因此:

    GET /files/some/file.csv

    将从/path/to/your/files/some/下载file.csv

    而且

    curl upload-file some-other-file.csv /files/some-other-file.csv

    将上载some-other-file.csv并将其存储在服务器上的/path/to/your/files/