Skip to content Skip to sidebar Skip to footer

How To Download File In Browser Using Spring Mvc?

I have file on the server & that I want to download on my machine using browser. But I am not getting an option from browser to download the file. My code is JSP
.

$scope.downloadFile = function(fileName) {
    window.location.href = 'logreport/downLoadFile?fileName=asdad1';
};

Solution 2:

I didn't have enough credit to give a comment so wiritting here.. Thanks user1298426. I was strugling like anything for this. I was trying with AJAX. With window.location.href, I can download file in browser...

MY javascript code is as follows:

jQuery('#exportToZip').click(function() {
    window.location.href = '*****';
});

*****: is the url mapping that I have in controller.

@RequestMapping(value = "/****")@ResponseBodypublicvoiddownloadRequesthandler(HttpServletRequest request,HttpServletResponse response) {
    String status;
    try {
        filedownloader.doGet(request, response); 
    } catch (ServletException e) {
        status="servlet Exception occured";
        e.printStackTrace();
    } catch (IOException e) {
        status="IO Exception occured";
        e.printStackTrace();
    }
    //return status;
}

publicvoiddoGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
    ServletContextcontext= request.getServletContext();

    // construct the complete absolute path of the fileFiledownloadFile=newFile(filePath);
    System.out.println("downloadFile path: "+ filePath);
    FileInputStreaminputStream=newFileInputStream(downloadFile);

    // get MIME type of the fileStringmimeType= context.getMimeType(fullPath);
    if (mimeType == null) {
        // set to binary type if MIME mapping not found
        mimeType = "application/octet-stream";
    }
    System.out.println("MIME type: " + mimeType);
    response.setContentLength((int) downloadFile.length());

    // set headers for the responseStringheaderKey="Content-Disposition";
    StringheaderValue= String.format("attachment; filename=\"%s\"",downloadFile.getName());
    response.setHeader(headerKey, headerValue);

    OutputStreamoutStream= response.getOutputStream();
    byte[] buffer = newbyte[BUFFER_SIZE];
    System.out.println("buffer: "+ buffer.length);
    intbytesRead= -1;

    // write bytes read from the input stream into the output stream//be carefull in this step. "writebeyondcontentlength" and "response already committed"  error is very common herewhile ((bytesRead = inputStream.read(buffer))!=-1  ) {

        outStream.write(buffer, 0, bytesRead);
    }

    inputStream.close();
    outStream.close();
}

Trying to download file with ajax is a blunder....

cheers......

Solution 3:

Instead of using IOUtils copy method

ServletOutputStream out = response.getOutputStream();
IOUtils.copy(in, out);

You can use following :

 ServletOutputStream out = response.getOutputStream();

 byte[] bytes = IOUtils.toByteArray(in);
 out.write(bytes);



 out.close();
 out.flush();

Hope this will work.

Post a Comment for "How To Download File In Browser Using Spring Mvc?"