How to download file from url using Spring MVC?

18,771

Try this:

@RequestMapping(value="/viewAttach", method = RequestMethod.GET)
public ModelAndView viewAttach(@RequestParam(value="article_id", required = true) String article_ref, HttpSession session, HttpServletResponse response) 
{

    /* *** Check Session *** */
    try {

        // Direclty from pier.content.GetContent written by ang94402

        URL url = new URL(binaryURL);           
        response.setHeader("Content-disposition", "attachment;filename=" + binary.getName());

        //Set the mime type for the response
        response.setContentType("application/pdf");

        // URLConnection connection = url.openConnection();
        InputStream is = url.openStream();

        BufferedOutputStream outs = new BufferedOutputStream(response.getOutputStream());
        int len;
        byte[] buf = new byte[1024];
        while ( (len = is.read(buf)) > 0 ) {
            outs.write(buf, 0, len);
        }
        outs.close();

    } catch (MalformedURLException e) {
        logger.error("Error ModelAndView.viewMain - MalformedURLException : " + e.toString() + " -- " + e.getStackTrace()[0].toString());
        return null;
    } catch (IOException e) {
        logger.error("Error ModelAndView.viewMain - IOException : " + e.toString() + " -- " + e.getStackTrace()[0].toString());
        return null;
    }


    return null;

}
Share:
18,771
Rahul
Author by

Rahul

Updated on June 04, 2022

Comments

  • Rahul
    Rahul almost 2 years

    I am having download option in my jsp like this

    <a href='<c:url value="/licensing/download.sp?name=${namelist.name}&downloadUrl=${namelist.url}"/>'>
    
    <img src="/images/download.gif" alt="Download" border="0" align="right">
    

    In the above "url" is the location of file and name is the file name.On click of download option in jsp iam calling the controller method download,in controller

    public ModelAndView download(HttpServletRequest request, HttpServletResponse response, DevTechBean devTechBean) throws Exception {
            cat.debug("MySuiteListController: download: begin");
            ModelAndView modelView = super.handleLicensingRequest(request, response);
            String name = request.getParameter("name");
    
            String url1 = request.getParameter("downloadUrl");
            cat.debug(" download: url ="+url1);
    
            String downloadurl1="https://my.net:8869"+url1;
            cat.debug(" download: downloadurl ="+downloadurl1);
        try{
            URL url = new URL(downloadurl1);  
            //response.setHeader("Content-Type", "text/csv");  
            response.setHeader("Content-disposition", "attachment;filename="+name);
            URLConnection connection = url.openConnection();
            InputStream stream = connection.getInputStream();
            BufferedOutputStream outs = new BufferedOutputStream(response.getOutputStream());
            int len;
            byte[] buf = new byte[1024];
            while ((len = stream.read(buf)) > 0) {
              outs.write(buf, 0, len);
            }
            outs.close();
        }
        catch (MalformedURLException e) { 
            cat.error("Error occurrred in url");
    
        } 
        catch (IOException e) { 
            cat.error("Error occurrred ");
    
        }
            String viewName = "swl_download";
        modelView.setViewName(viewName);
    return modelView;       
    
    }
    

    But when i click on download i am getting file not found exception. Iam thinking that problem is due to the url value. In the above iam having value of downloadurl=/files/download/hai.txt

    when i give

    <a href="${namelist.url}"/>
    <img src="/images/download.gif" alt="Download" border="0" align="right"></a><br/><br/></td>
    

    on click the file is opening in browser with the url https://my.net:8869//files/download/hai.txt(but here for href iam giving only this link "/files/download/hai.txt" dont know how the entire link is coming.

    but if give link like this to call the controller for opening that file as pop up.

    <a href='<c:url value="/download.sp?name=${namelist.name}&downloadUrl=${namelist.url}"/>'>
    

    it is getting file not found exception. I think it is due that do downloadUrl.so i have added like this in above

    String downloadurl1="https://my.net:8869"+url1;
    

    But i am getting file not find exception.Please help me resolving this.