I am developing a Rest API using Spring Boot and AngularJS in the client side, i am uploading files to /resources/static/upload with Spring using the RestController below and using them in the client side
@RestController
@CrossOrigin("*")
public class FilmController {
@Autowired
private FilmRepository filmRepository;
@RequestMapping(value = "/films", method = RequestMethod.POST)
public void saveFilm(@RequestParam("file") MultipartFile file) throws Exception {
File convFile = new File("src/main/resources/static/upload/"+file.getOriginalFilename());
convFile.createNewFile();
FileOutputStream fos = new FileOutputStream(convFile);
Blob blob = new SerialBlob(file.getBytes());
fos.write(file.getBytes());
fos.close();
System.out.println(convFile.getName());
filmRepository.save(new Film(convFile.getName(), blob));
}
@RequestMapping(value = "/films", method = RequestMethod.GET)
public List<Film> getAllFilms() {
return filmRepository.findAll();
}
}
Here is how i accessed the uploaded image "image.jpg"
<img src="http://localhost:8080/upload/image.jpg" />
But, when i ran mvn package and i launch my application jar file, i can't access the uploaded image in the client side, i get 404 not found.
Can someone explain how Spring store and refer to static resources and how can i resolve this problem.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…