2

我的jsp中有这样的下载选项

<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中的下载选项,调用控制器方法下载,在控制器中

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;       

}

但是当我点击下载时,我得到文件未找到异常。我认为这个问题是由于 url 值。在上面的 iam 中,值为 downloadurl=/files/download/hai.txt

当我给

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

单击时,文件将在浏览器中打开,网址为https://my.net:8869//files/download/hai.txt (但此处为 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;

但我得到文件找不到异常。请帮我解决这个问题。

4

2 回答 2

1

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;

}
于 2013-06-27T14:32:03.960 回答
0

这只是一个伪代码。根据您的需要更改它。

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

首先尝试找出 getClass() 指向哪个目录。(不确定,但它应该是您的应用程序的 HOME ?)。然后将您的文件放在相同的位置,如果不是。

希望这会有所帮助。 资源

于 2012-06-12T06:43:27.093 回答