基于springMvc框架下的文件下载
2017-03-06 11:00
211 查看
/**
* 文件下载
* @throws UnsupportedEncodingException
*/
@RequestMapping(value =
"/download",method = RequestMethod.GET)
public String download(String fileName,HttpServletRequest request,HttpServletResponse response)
throws UnsupportedEncodingException{
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
String str=new String(fileName.getBytes("ISO-8859-1"),"UTF-8");
response.setHeader("Content-Disposition","attachment;fileName="+str);
try {
String path=request.getSession().getServletContext().getRealPath("upload");//文件存放在这个目录下
InputStream inputStream=new FileInputStream(new File(path
+ File.separator+str));
OutputStream os = response.getOutputStream();
byte[] b =
new byte[2048];
int length;
while((length = inputStream.read(b))>0){
os.write(b,0,length);
}
os.close();
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
return null;
}
前台页面只需要个链接
<a href="${base}/product/download.jhtml?fileName=${product.fileName}"></a>
* 文件下载
* @throws UnsupportedEncodingException
*/
@RequestMapping(value =
"/download",method = RequestMethod.GET)
public String download(String fileName,HttpServletRequest request,HttpServletResponse response)
throws UnsupportedEncodingException{
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
String str=new String(fileName.getBytes("ISO-8859-1"),"UTF-8");
response.setHeader("Content-Disposition","attachment;fileName="+str);
try {
String path=request.getSession().getServletContext().getRealPath("upload");//文件存放在这个目录下
InputStream inputStream=new FileInputStream(new File(path
+ File.separator+str));
OutputStream os = response.getOutputStream();
byte[] b =
new byte[2048];
int length;
while((length = inputStream.read(b))>0){
os.write(b,0,length);
}
os.close();
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
return null;
}
前台页面只需要个链接
<a href="${base}/product/download.jhtml?fileName=${product.fileName}"></a>
相关文章推荐
- Android-- 基于http的文件下载开源框架file-downloader
- 基于SpringMVC的文件上传和下载
- SSM框架-SpringMVC 实例文件上传下载
- 基于SpringMVC的文件上传下载功能
- SSM框架-SpringMVC 实例文件上传下载
- 一个基于Retrofit的单文件上传、下载框架
- 利用SpringMVC框架提供的方法,做文件上传和下载操作
- SpringMVC框架(四)文件的上传下载,上下文路径
- 基于springmvc的文件上传与下载
- 使用springMVC框架实现文件下载
- 在SpringMVC框架下实现文件的上传和下载示例
- Springmvc基础框架搭建流程(1)-基于xml配置文件
- springmvc文件上传下载简单实现案例(ssm框架使用)
- 基于SpringMVC的文件下载实例
- 基于SpringMVC的文件下载实例
- java文件上传与下载(springmvc框架下)及enctype=”multipart/form-date导致的中文乱码的解决
- 框架学习之springMvc整合框架-02文件上传、下载、国际化、拦截器、异常框架
- 【框架学习】SpringMVC上传下载文件
- 在SpringMVC框架中实现文件上传和下载
- SSM框架-SpringMVC 实例文件上传下载