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
OS:Windows8.1 32bit eclipse:4.5.2
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.
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