Server gives 500 code error but still serves content

Waseem

I'm serving "serveraddress/img/:imageid" with this function:

 public static String getImage(Request req, Response res) {
        String imageId = req.params("imageid");
        byte[] bytes = readImage("img/" + imageId);

        return Base64.getMimeEncoder().encodeToString(bytes); 
    }

 public static byte[] readImage(String image) {
        byte[] bytes;
        try {
            File fff = new File(image);
            FileInputStream fileInputStream = new FileInputStream(fff);
            long byteLength = fff.length();

            bytes = new byte[(int) byteLength];
            fileInputStream.read(bytes, 0, (int) byteLength);
        } catch (Exception ex) {
            ex.printStackTrace();
            bytes = null;
        }
        return bytes;
    }

For the first couple of requests, it delivers normally. But when a couple more, like 5 or 6 requests ask for images it starts sending out server error code 500 without any stack trace. I'm not sure what the issue is, but I hope someone can help me figure out.

Here is my gradle.build file for anyone who wants to test it out:

testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.21'
compile "com.sparkjava:spark-core:2.7.1"

also this is the main function:

public static void main(String[] args) {
        BasicConfigurator.configure();
        org.apache.log4j.Logger.getRootLogger().setLevel(Level.ERROR);
        port(8883);
        get("/img/:imageid", this::getImage);
}
Mithfindel

It looks like your code never closes the File nor the InputStream that it uses, so you're leaking file descriptors and memory. You should use a try-with-resources or close them in a finally block (and on a side note, maybe you could use a linter to warn you about unclosed resources).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Creating symlink gives 500 Internal Server Error

PHP MySql insert gives server error 500

After hosting to server symfony gives 500 internal server error

How to fix an error in a php variable? Server gives 500 error

Jupyer lab gives Error 500: Internal Server Error

Ajax call to webmethod gives ERROR 500 Internal server error

simple AJAX JQuery example gives me 500 internal server error

Selenium gives Server Error 500 after click (Django, pytest)

Django, nginx, gunicorn: why on some page gives Server Error (500)?

Excel/PowerBI OData import gives internal server error 500

ExpressJS API Deployed on Heroku Gives 500 Internal Server Error

Htaccess rewrite url gives 500 Internal server error

Apache server mod_wsgi in Redhat gives 500 error

PHP code giving 500 internal server error

SQL Server gives error to unreachable code

Error 500 internal server error when posting long forms content

{{csrf_token}} gives me 403 Forbidden and {%csrf_token%} gives me 500 Server Error

The development server returned response error code: 500 on a boiler plate code

RewriteRule gives HTTP 500 error

Gives an error but still runs properly

Laravel storage disk put gives internal server error 500 on live server | no logs are created

local server displays yajra laravel datatables correctly, remote server gives error 500

Whenever debug=False in django, Heroku gives Server Error (500) and when debug=True no error

Laravel - Jquery Dynamic Drop Down gives error 500 internal server error

The development server returned response error code: 500 in react native

AFNetworkingErrorDomain Code=-1011 "Request failed: internal server error (500)"

The development server returned response error code: 500 react-native

500 Internal Server Error Given even though code successfully completes

Internal server error code 500 when uploading a file to a directory with Flask