繁体   English   中英

春季启动上传永久文件

[英]spring boot uploading permanent files

我尝试使用Spring Boot上传文件,但是每次重新启动服务器时,上传的文件都会消失。 spring boot创建一个名为upload-dir文件,每次我重新启动服务器时,该文件都会被删除并重新创建,所以我不知道在哪里上传和存储我的文件。

我的文件上传控制器代码:

package com.theligue.webservice;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.theligue.webservice.storage.StorageFileNotFoundException;
import com.theligue.webservice.storage.StorageService;

import java.io.IOException;
import java.util.stream.Collectors;

@Controller
public class FileUploadController {

    private final StorageService storageService;

    @Autowired
    public FileUploadController(StorageService storageService) {
        this.storageService = storageService;
    }

    @GetMapping("/uploadPOC")
    public String listUploadedFiles(Model model) throws IOException {
        model.addAttribute("files", storageService
                .loadAll()
                .map(path ->
                        MvcUriComponentsBuilder
                                .fromMethodName(FileUploadController.class, "serveFile", path.getFileName().toString())
                                .build().toString())
                .collect(Collectors.toList()));
        return "uploadForm";
    }





    @GetMapping("/files/{filename:.+}")
    @ResponseBody
    public ResponseEntity<Resource> serveFile(@PathVariable String filename) {
        Resource file = storageService.loadAsResource(filename);
        return ResponseEntity
                .ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\""+file.getFilename()+"\"")
                .body(file);
    }

    @PostMapping("/")
    public String handleFileUpload(@RequestParam("file") MultipartFile file,
                                   RedirectAttributes redirectAttributes) {
        storageService.store(file); 
        redirectAttributes.addFlashAttribute("message",
                "You successfully uploaded " + file.getOriginalFilename() + "!");

        return "redirect:/uploadPOC/";
    }

    @ExceptionHandler(StorageFileNotFoundException.class)
    public ResponseEntity handleStorageFileNotFound(StorageFileNotFoundException exc) {
        return ResponseEntity.notFound().build();
    }

}

我认为您使用的是Spring Boot示例? 因此,您只需要在Application.init()中删除storageService.deleteAll()调用即可,该调用会在启动时删除所有上载的文件。

假设您是要上传文件的MYSQL数据库,我希望您尝试以下方法。 在您的WEB-INF文件夹下,您应该具有spring-servlet文件。 在该文件中,您将定义休眠属性。 它应该类似于以下代码。

<property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>

当您将属性保留为“创建”时,则每次启动服务器时,它必然会重新创建表并一次又一次上载文件。 为了将新文件保存到数据库中,同时保持旧文件不变,请执行以下操作。

更换

<prop key="hibernate.hbm2ddl.auto">create</prop>

<prop key="hibernate.hbm2ddl.auto">update</prop>

而已。 现在,将不更新表,而是更新数据库中的文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM