简体   繁体   中英

Image getting 403 forbidden when deployed to server

Background: I am using Spring web MVC along with JSP and HTML/JavaScript to write a website. I have added a search function, which sends a query to imdbapi.org and receives a JSON object containing movie/tv show information (via AJAX). The JSON object contains a "poster" field for each result, which is a URL to an image (on imdb server). The results are then displayed showing the poster if available, using jQuery..

someDiv.append($("<img src='"+results[i].poster+"'></img'").addClass("resultImg"));

Problem: This works absolutely fine when running on STS built-in server and accessing locally eg.

http://localhost:8080/myWebPage.whatever

I have a ubuntu server box with Tomcat 7 installed, when deploying to the server I get a 403 error for each image. Example from chrome console:

GET http://ia.media-imdb.com/images/M/MV5BMTY2NDY4NDA0OV5BMl5BanBnXkFtZTcwNTI4MzUyMQ@@._V1._SY317_CR5,0,214,317_.jpg 403 (Forbidden) 

More Info: Have tested in chrome and firefox with same results. If I cannot fix then one solution would be to download to a temp folder on the server I think...

Is this a configuration problem on my box with the tomcat server?

Is this a configuration problem on my box with the tomcat server?

No, Not at all, Your server configuration has nothing to do with the external resource accessibility,If you are getting @._V1._SY317_CR5,0,214,317_.jpg 403 (Forbidden) - this clearly mean's that the server is refusing your request.

And it seem's many people's have these kind of problem with IMDB - see this

I have "made it work", it is not a direct solution to the question but a workaround. Using a local proxy on my server I download the image into memory in java and then return to the web page...

Java (server side)

@RequestMapping(value="/pages/proxyImg")
public ResponseEntity<byte[]> proxyImage(String url) {
    log.info("Image Proxy server: " + url);
    try {
        final HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_JPEG);
        return new ResponseEntity<byte[]>(IOUtils.toByteArray(new URL(url).openConnection().getInputStream()), headers, HttpStatus.CREATED);
    } catch(Exception e) {
        e.printStackTrace();
        return null;
    }
}

Javascript

someDiv.append($("<img src='"+"proxyImg.htm?url="+results[i].poster+"'></img'"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM