Display an error screen during the download process to response

Summary of this article

Describe the points that were difficult in error handling when implementing the file download function in java.

[Point] Do not automatically close the output stream Close only at the end of normal Reset response with catch statement and don't close

environment

OS:Windows8.1 32bit eclipse:4.5.2

Source that cannot display the error screen

In the original source, the close process was described in the try-catch-resouses statement and finally clause.


  try {
    //Get the input stream of the file to be downloaded
        //Get output stream from response
    //Write from the input stream to the output stream
  } catch(Exception e) {
    //Exception handling
  } finally {
    //Close processing
  }

}

With this source, there is no problem when it ends normally, but since the response is closed even when an exception occurs, files that are not normal when viewed from the client side are downloaded.

Therefore, make the following modifications.

Source to display error screen


  try {
    //Get the input stream of the file to be downloaded
        //Get output stream from response
    //Write from the input stream to the output stream
    //Close processing only at the time of normal termination
  } catch(Exception e) {
        //response reset process
    //Exception handling
  } 

}

With this, it will be downloaded only when it ends normally, and an error screen can be displayed when an exception occurs.

Recommended Posts

Display an error screen during the download process to response
Display an image on the base64 screen
I want to display an error message when registering in the database
[No.007] Organization management screen and login process to the organization
[Rails] How to display an image in the view
How to display products by category on the same list screen
[Rails 5] How to display the password change screen when using devise
Processing to issue an error message
What to do if you get an error during rails db: reset
[Rails] About the error when displaying the screen due to the autofocus of the form
[Swing] How to display an arbitrary name on the menu bar on Mac
Rails tutorial Resolved an error deploying to Heroku, so note the solution
How to put out the error bundling
[Rails] How to display error messages individually
How to pass the value to another screen
The process of introducing Vuetify to Rails
Let's make an error screen with Rails
Pre-processing to display on the browser (compiler)
How to display error messages in Japanese
[Error] How to resolve the event that the screen does not transition after editing