2017-04-15 5 views
-1

ユーザーが確認の前にプレビューを表示するためにアップロードした後、数分間画像を保存する必要があります。春のブートストアのアップロードファイル

私はこれを行うための最良の方法を知りたいと思います。

私はキャッシュとカフェインについて見ましたが、これは

後にそれを取り戻すために、ランダムなハッシュとキャッシュで最高のpraticeとどの店であれば、私は知らない[​​EDIT]

たぶん私は問題を過大評価していたかもしれません

@ロバーツの提案に続いて、私は一時ファイルを使用しますが、私はまだファイルを削除するための何らかの方法が必要です。だから私は新しい質問を作成し、私はこれらの用語で検索する他の人を助けるためにこれを保持します。

は、私は私のアプリケーションの一つでこれを行う

How guarantee the file will be deleted after automatically some time?

+0

を使用すると、HTTP応答から画像を取得していますか?または何? –

+0

はい、HTTP応答 –

+0

すべての画像を保持し、確認されていない画像を削除してみませんか? 2つの異なる手法を使用するよりも簡単にできます。 – Marged

答えて

0

リンクに従ってください。

  • アップロードPOSTでは、イメージを一時ファイルに保存し、セッション属性に一時ファイル名を保存します。プレビューされるイメージは、永続ストレージに書き込まれるまで他のユーザーには表示されないようにするため、セッション属性を使用します。
  • 次のGETでは、一時ファイル名をセッションから取り出し、応答にストリームして終了したら削除します。私はもうプレビューがレンダリングされた後にファイルを保持してもらう必要はありません。

以下の完全な実装を参照してください:

import java.io.IOException; 
import java.io.OutputStream; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession; 

import org.springframework.http.HttpStatus; 
import org.springframework.http.ResponseEntity; 
import org.springframework.web.bind.annotation.GetMapping; 
import org.springframework.web.bind.annotation.PostMapping; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestPart; 
import org.springframework.web.bind.annotation.RestController; 
import org.springframework.web.multipart.MultipartFile; 

@RestController 
@RequestMapping("/api/imagePreview") 
public class ImagePreviewController 
{ 
    @PostMapping 
    public ResponseEntity<?> post(HttpSession session, @RequestPart MultipartFile file) throws IOException 
    { 
     if (file.getContentType() != null && file.getContentType().startsWith("image/")) { 
      Path tempFile = Files.createTempFile("", file.getOriginalFilename()); 
      file.transferTo(tempFile.toFile()); 
      session.setAttribute("previewImage", tempFile.toFile().getPath()); 
      session.setAttribute("previewImageContentType", file.getContentType()); 
      return ResponseEntity.status(HttpStatus.CREATED).build(); 
     } else { 
      return ResponseEntity.status(HttpStatus.UNSUPPORTED_MEDIA_TYPE).build(); 
     } 
    } 

    @GetMapping 
    public void get(HttpServletRequest request, HttpServletResponse response) throws IOException 
    { 
     HttpSession session = request.getSession(false); 
     if (session == null) { 
      response.sendError(HttpServletResponse.SC_NOT_FOUND); 
      return; 
     } 
     String path = (String) session.getAttribute("previewImage"); 
     String contentType = (String) session.getAttribute("previewImageContentType"); 
     if (path == null || contentType == null) { 
      response.sendError(HttpServletResponse.SC_NOT_FOUND); 
      return; 
     } 
     response.setContentType(contentType); 
     try (OutputStream out = response.getOutputStream()) { 
      Files.copy(Paths.get(path), out); 
     } finally { 
      Files.deleteIfExists(Paths.get(path)); 
     } 
    } 
} 
+0

そうですが、ユーザーがgetメソッドを呼び出さないとどうしますか?このファイルが削除されるときは?これは私の問題です。どうすればファイルが削除されるのを確認できますか? idea @robertに感謝します。 –

+0

一時ファイルを使った作業に関する別の質問がありました。http://stackoverflow.com/questions/43483750/how-guarantee-the-file-will-be-deleted-after-automatically-some-time –

関連する問題