title: How to download file from url using Spring MVC?
body: <p>I am having download option in my jsp like this</p>
  
  <pre><code>&lt;a href='&lt;c:url value="/licensing/download.sp?name=${namelist.name}&amp;downloadUrl=${namelist.url}"/&gt;'&gt;
  
  &lt;img src="/images/download.gif" alt="Download" border="0" align="right"&gt;
  </code></pre>
  
  <p>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</p>
  
  <pre><code>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)) &gt; 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;       
  
  }
  </code></pre>
  
  <p>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</p>
  
  <p>when i give</p>
  
  <pre><code>&lt;a href="${namelist.url}"/&gt;
  &lt;img src="/images/download.gif" alt="Download" border="0" align="right"&gt;&lt;/a&gt;&lt;br/&gt;&lt;br/&gt;&lt;/td&gt;
  </code></pre>
  
  <p>on click the file is opening in browser with the url <a href="https://my.net:8869//files/download/hai.txt(but" rel="nofollow">https://my.net:8869//files/download/hai.txt(but</a> here for href iam giving only this link "/files/download/hai.txt" dont know how the entire link is coming.</p>
  
  <p>but if give link like this to call the controller for opening that file as pop up.</p>
  
  <pre><code>&lt;a href='&lt;c:url value="/download.sp?name=${namelist.name}&amp;downloadUrl=${namelist.url}"/&gt;'&gt;
  </code></pre>
  
  <p>it is getting file not found exception.
  I think it is due that do downloadUrl.so i have added like this in above</p>
  
  <pre><code>String downloadurl1="https://my.net:8869"+url1;
  </code></pre>
  
  <p>But i am getting file not find exception.Please help me resolving this.</p>