2017-01-27 5 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フォルダの下に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">update</prop> 

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

を交換してください。テーブルの再作成の代わりに、データベースにファイルの更新があります。

関連する問題