luca luca - 4 months ago 14
reST (reStructuredText) Question

Set message into file download blank page returned from web service

I have this web service for file download from email (I provide URL for download):

@RequestMapping(value = { "download"}, method = RequestMethod.GET)
public void downloadFile(HttpServletResponse response, @RequestParam(value="filePath", required = true) String filePath) {
File file = new File(filePath);
if (file.isFile()){
try(InputStream is = new FileInputStream(file.getAbsolutePath())){
setResponse(response, is, file, "attachment; filename=");
}catch (Exception e1){
ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e1);
LOG.error("Threw exception in FleetAndCarControllerImpl::downloadStatisticalFile :" + errorResponse.getStacktrace());
//TODO write that file is not present
}catch (Exception e) {
ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e);
LOG.error("Threw exception in FleetAndCarControllerImpl::downloadStatisticalFile :" + errorResponse.getStacktrace());

and setResponse method:

private void setResponse(HttpServletResponse response, InputStream is, File file, String useOfFile) throws Exception{
response.setHeader("Expires", "0");
response.setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
response.setHeader("Content-Description"," File Transfer");
response.setHeader("Content-Disposition", useOfFile + file.getName());
response.setHeader("Content-Transfer-Encoding"," binary");
long size =, response.getOutputStream());
response.addHeader("Content-Length", Long.toString(size));

It works fine when file exists, now I would like to write a message if file doesn't exist because now the user views a blank white page and it doesn't know what is happening.
Can you help me?


You could do something like:

response.sendError(HttpServletResponse.SC_NOT_FOUND, "File was not found");

Or better send a redirect to a page with an error message/explanations on what to do next.