2016-10-05 4 views
0

私はアバター画像をアップロードして表示する機能を開発しましたが、アバターは変わらず、画像はすでにフォルダ内で変更されています。アバターイメージを更新して表示しますが、アバターはスプリングブートで変更されません。なぜですか?

問題はhttpキャッシュですか?

私の単純なプロジェクト:

dropbox link to my project

(注意:あなたがTestingControllerでローカルパスを変更する必要があります)

+0

> "問題は、httpキャッシュのですか?"おそらく - ウェブページをリロードするとどうなりますか(Ctrl + F5を押すか、Ctrl + Shift + R(Windows、Linux)を押してください) – Ralph

+0

@ Ralph、私はページを更新してイメージのURLを開こうとしますが、画像は古い画像 –

+0

どのフォルダに画像を保存しますか?アプリケーションの内部または外部? – Patrick

答えて

0

あなたは画像を保存するので、あなたは即座にアップロードされた画像を見ることができませんアプリケーションの静的ファイルフォルダに保存された画像を見ることはできますが、あなたがあなたのURLを呼び出した場合は見ることができません。ファイルをアップロードした後(プロジェクトルートとF5キーを押した後)、実行中のプロジェクトをリフレッシュすると、それを見ることができます。

ただし、画像を読み込んでブラウザに表示すると、より良い解決策が得られます。@RequestMapping

プロジェクトのビルド、このようにそれを使用しよう:このコードで

import java.io.IOException; 
import java.net.MalformedURLException; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.util.stream.Stream; 

import org.json.JSONObject; 
import org.springframework.core.io.Resource; 
import org.springframework.core.io.UrlResource; 
import org.springframework.http.ResponseEntity; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.GetMapping; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.bind.annotation.ResponseBody; 
import org.springframework.web.multipart.MultipartFile; 

@Controller 
public class TestingController { 

    private final Path p = Paths.get("/Users/devin/Documents/workspace-sts-3.8.1.RELEASE/testing/src/main/resources/static/uploads/images"); 

    @RequestMapping(value={ "/", "/home"}) 
    public String home(){ 
     return "home"; 
    } 

    @RequestMapping(value = "/post_upload_avatar_file", method = RequestMethod.POST) 
    @ResponseBody 
    public Object uploadAvatarFile(@RequestParam("uploadfile") MultipartFile uploadfile) { 
     JSONObject resJsonData=new JSONObject(); 
     try { 
      if(uploadfile.isEmpty()){ 
       System.out.println("Empty"); 
      } 

      Files.copy(uploadfile.getInputStream(), p.resolve(uploadfile.getOriginalFilename())); 

      resJsonData.put("status", 200); 
      resJsonData.put("message", "Success!"); 
      resJsonData.put("data", uploadfile.getOriginalFilename()); 
     }catch (Exception e) { 
      System.out.println(e.getMessage()); 
      resJsonData.put("status", 400); 
      resJsonData.put("message", "Upload Image Error!"); 
      resJsonData.put("data", ""); 
     } 
     return resJsonData.toString(); 
    } 

    @GetMapping("files/{filename:.+}") 
    @ResponseBody 
    public ResponseEntity<Resource> serverFile(@PathVariable String filename){ 
     Resource file = loadAsResource(filename); 
     return ResponseEntity 
       .ok() 
       .body(file); 
    } 

    public Resource loadAsResource(String filename) { 
     try { 
      Path file = p.resolve(filename); 
      Resource resource = new UrlResource(file.toUri()); 
      if(resource.exists() || resource.isReadable()) { 
       return resource; 
      } 
      else { 
       System.out.println("no file"); 
      } 
     } catch (MalformedURLException e) { 
      System.out.println(e); 
     } 
     return null; 
    } 

    public Stream<Path> loadAll() { 
     try { 
      return Files.walk(p, 1) 
        .filter(path -> !path.equals(p)) 
        .map(path -> p.relativize(path)); 
     } catch (IOException e) { 
      System.out.println(e); 
     } 
     return null; 
    } 
} 

私は例外とエラー処理を実装してはいけません。

写真をそのままアップロードすることができます。そしてあなたのブラウザで画像を受け取るために別のURLを呼び出すことができます。

​​

、画像が応答する必要があります。

完全な解決方法については、これら2つの情報源をご覧ください。

Spring file upload - backend

Spring file upload - frontend

+0

それは私の仕事です、ありがとう –

関連する問題