我在我的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”是文件的位置,name是文件名。点击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(but,此处为href iam仅提供此链接“/files/download/hai.txt”不知道整个链接的来源。
但如果给这样的链接调用控制器打开该文件弹出。
<a href='<c:url value="/download.sp?name=${namelist.name}&downloadUrl=${namelist.url}"/>'>
它正在获取文件未找到异常。 我认为这是由于我在上面添加了这样的内容
String downloadurl1="https://my.net:8869"+url1;
但是我得到的文件没有找到异常。请帮我解决这个问题。
答案 0 :(得分: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;
}
答案 1 :(得分:0)
这只是一个伪代码。根据您的需要进行更改。
InputStream is = getClass().getResourceAsStream("filename");
首先尝试弄清楚getClass()指向哪个目录。(不确定,但它应该是你的App的HOME?)。然后将文件放在相同的位置,如果不是。
希望这会有所帮助。 source