2017-04-15 41 views
-1

我需要在用戶上傳圖片以顯示預覽,確認之前,確認後我需要取回並保留圖片幾分鐘。Spring Boot商店上傳文件

我想知道做這件事的最佳實踐。

我看到了有關Cache和咖啡因,但我不知道這是不是最好的初步實踐,以及如何在高速緩存存儲與隨機哈希後拿回來

[編輯]

也許我高估了這個問題

@Robert建議我會使用臨時文件,但我仍然需要一些方法來保證文件將被刪除。所以我創建了一個新問題,我會繼續幫助其他人用這些術語進行搜索。

按照鏈接

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

+0

你得到的HTTP響應的形象呢?或者是什麼? –

+0

是的,來自HTTP響應 –

+0

爲什麼不保留所有圖像並刪除那些未確認的圖像?可能比涉及兩種不同的技術更簡單。 – 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方法時,你會做什麼?當這個文件將被刪除?這是我的問題,我如何確定該文件將被刪除? –

+0

感謝您的想法@robert。我創建了另一個關於使用臨時文件的問題http://stackoverflow.com/questions/43483750/how-guarantee-the-file-will-be-deleted-after-automatically-some-time –