问题描述
< a href ='< c:url value = /licensing/download.sp?name=${namelist.name}&downloadUrl=${namelist.url}\"/>'>
< img src =/ images / download.gifalt =下载border =0align =right>
上面的url是文件的位置,名称是文件名。点击下载选项在jsp iam调用控制器方法下载,在控制器中
public ModelAndView下载(HttpServletRequest请求,HttpServletResponse响应,DevTechBean devTechBean) throws Exception {
cat.debug(MySuiteListController:download:begin);
ModelAndView modelView = super.handleLicensingRequest(请求,响应);
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();
BufferedOutputStreamouts = 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(在url中出现错误);
}
catch(IOException e){
cat.error(Error occurredrred);
}
String viewName =swl_download;
modelView.setViewName(viewName);
return modelView;
}
但是当我点击下载我得到文件没有找到例外。
Iam认为问题是由于url值。
在上述iam的值为downloadurl = / files / download / hai.txt
当我给
< a href =$ {namelist.url}/>
< img src =/ images / download.gifalt =下载border =0align =right>< / a>< br />< br /> ;< / TD>
点击该文件在浏览器中打开url 这里是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帮我解决这个。
尝试这个:
@RequestMapping(valu e =/ viewAttach,method = RequestMethod.GET)
public ModelAndView viewAttach(@RequestParam(value =article_id,required = true)String article_ref,HttpSession会话,HttpServletResponse响应
{
/ * ***检查会话*** * /
尝试{
//来自pier.content.GetContent的Direclty由ang94402
URL url = new URL(binaryURL);
response.setHeader(Content-disposition,attachment; filename =+ binary.getName());
//设置响应的mime类型
response.setContentType(application / pdf);
// URLConnection connection = url.openConnection();
InputStream is = url.openStream();
BufferedOutputStreamouts = 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());
返回null;
} catch(IOException e){
logger.error(Error ModelAndView.viewMain - IOException:+ e.toString()+ - + e.getStackTrace()[0] .toString ());
返回null;
}
返回null;
}
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.
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;
}
这篇关于如何使用Spring MVC从url下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!