Im Projekt werden die hochgeladenen Dateien von sha1 umbenannt. Nachdem ein Bild hochgeladen wurde, hei?t es c2e47454e951697811c0531949d4d318. Der Zugriffspfad auf der Browserseite lautet: /img/c2e47454e951697811c0531949d4d318 für das Bild!
怎么在nignx里配置映射到真實(shí)的 image_save_path/img/c2/e4/7454e951697811c0531949d4d318.[jpg|gif|png]
location ~/img/*$ {
root image_real_path_here;
# 此處不太理解該如何處理圖片名稱的前四個(gè)字符,變成路徑形式,
}
Bitte hilf mir sehr! Danke
走同樣的路,發(fā)現(xiàn)不同的人生
首先,你既然已經(jīng)將其sha1過了,就不需要再存儲其后綴名了,存儲時(shí)將所有都改變?yōu)閟ha1后的值
然后,如果直接訪問Nginx來獲取圖片的話是得不到正確的文件名的,有些其他的邏輯也不好處理,所以干脆寫一個(gè)controller來做處理:
@RequestMapping(value = "imgs/{sha1}/download", method = RequestMethod.GET)
@ResponseBody
public HttpEntity<byte[]> downloadAttachment(@PathVariable("sha1") String sha1)
throws UnsupportedEncodingException {
Attachment attachment = attachmentService.getBySha1(sha1);
if (attachment == null) {
throw new ResourceNotFoundException();
}
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.APPLICATION_OCTET_STREAM);
String filename = new String(attachment.getName().getBytes("GB2312"), "ISO_8859_1");
header.setContentDispositionFormData("attachment", filename);
header.add("X-Accel-Redirect", String.format("/img/c2/e4/%s", sha1));
header.add("X-Accel-Charset", "utf-8");
return new HttpEntity<byte[]>(null, header);
}
這樣既可以使用Nginx的緩存,又可以使用自己的代碼做一些邏輯操作,還可以添加權(quán)限判斷的功能
另外Nginx的配置不需要改變