2012-06-12 55 views
2

我有下載選項在我的JSP中如何使用Spring MVC從url下載文件?

<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"> 

在上面的「URL」的文件名的位置在JSP IAM調用控制器方法下載的下載選項的文件name.On點擊,在控制器中

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;  

} 

但是當我點擊下載時我收到文件沒有找到異常。Iam認爲問題是由於url值。 在downloadurl = /文件/下載/ hai.txt上述IAM具有價值

當我給

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

上單擊該文件在瀏覽器的URL https://my.net:8869//files/download/hai.txt(but打開這裏的href IAM只給這個鏈接「/files/download/hai.txt」不知道整個鏈接是如何來的。

但如果給這樣的鏈接調用控制器打開該文件彈出。

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

它得到文件未找到異常。 我認爲這是由於那些downloadUrl.so我添加了一些這樣的上述

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

但我得到的文件找不到exception.Please幫我解決這個。

+0

缺少的東西上線1 .. –

+0

@FelixChristy:謝謝,我已經編輯 – Rahul

+0

我會用'的getResourceAsStream()'來獲得txt文件。 – Ved

回答

1

這只是一個僞代碼。根據您的需求更改它。

InputStream is = getClass().getResourceAsStream("filename"); 

首先試着找出getClass()指向哪個目錄(不知道,但它應該是你的應用程序的主頁?)。然後將您的文件放入相同的位置,如果沒有。

希望這會有所幫助。 source

1

試試這個:

@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; 

}