2017-01-27 2 views
0

봄 부팅을 사용하여 파일을 업로드하려고하지만 서버를 다시 시작할 때마다 내가 업로드 한 파일이 사라집니다. 스프링 부트는 이름이 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(); 
    } 

} 

답변

2

스프링 부트 예제를 사용하고 있다고 생각하십니까? 따라서 Application.init()에서 storageService.deleteAll() 호출을 제거하면 시작시 업로드 된 모든 파일이 삭제됩니다.

+0

근무 해 주셔서 감사합니다. 디렉토리에 파일을 업로드하는 방법을 알고 있습니까? – mohammad

1

당신이 당신의 파일을 업로드 할 MySQL 데이터베이스 있다고 가정 할 때, 나는 다음과 같은 방법을 시도 할 수 싶습니다. WEB-INF 폴더 아래에 스프링 서블릿 파일이 있어야합니다. 이 파일에서 당신은 최대 절전 모드 속성들을 정의했을 것이다. 다음 코드와 같아야합니다.

<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">update</prop> 

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

를 교체합니다. 이제 테이블을 재 작성하는 대신 데이터베이스에 파일을 갱신 할 것입니다.