下载代码参考
来源:互联网 发布:申请域名多少钱 编辑:程序博客网 时间:2024/04/30 01:29
jsp页面传值:
function getBasePath(){ var obj=window.location; var contextPath=obj.pathname.split("/")[1]; var basePath=obj.protocol+"//"+obj.host+"/"+contextPath; return basePath; }var methodPath = getBasePath();document.getElementById("exportImg").href = methodPath+ "/download/downLoadFile.do?filePath=/resources/images/linepic.png&filename=linepic.png";
controller层代码:
package com.sunyway.govhr.utils;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/download")public class DownLoad {@RequestMapping(value="/downLoadFile.do")public void downLoadFile(HttpServletResponse response,HttpServletRequest request,String filePath,String filename) throws IOException {BufferedOutputStream bos = null;BufferedInputStream bis = null;String path = request.getSession().getServletContext().getRealPath("/")+filePath;//request.getSession().getServletContext() 获取的是Servlet容器对象,相当于tomcat容器了。getRealPath("/") 获取实际路径,“/”指代项目根目录,所以代码返回的是项目在容器中的实际发布运行的根路径如:I:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\UMPWeb_20131230\File downfile = new File(path);try {response.setContentType("application/x-msdownload");response.setHeader("Content-disposition", "attachment;filename="+filename); bis = new BufferedInputStream(new FileInputStream(downfile));bos = new BufferedOutputStream(response.getOutputStream());byte[] buff = new byte[1024];int bytesRead;while(-1 != (bytesRead = bis.read(buff,0,buff.length))) {bos.write(buff,0,bytesRead);}} catch (Exception e) {} finally {if (bis != null) {bis.close();}if (bos != null) {bos.close();}}}}
0 0
- 下载代码参考
- 多线程复制下载文件参考代码
- 附件(blob)下载输出实例代码参考
- 代码参考
- 代码参考
- 图片上传代码参考。
- WebCasr邮件代码参考
- DataGrid常用代码参考
- 参考暂存 方法 代码
- JACL参考代码网址
- html 代码大全 参考
- 代码参考url
- iLBC 参考代码
- 粘贴别人代码--参考
- strtok使用代码参考
- 画十字参考代码
- google earth参考代码
- hpple参考代码
- android打包自动生成build
- 移植boa服务器
- cocos2d-x 3.2 屏幕适配
- LA 4670 Dominating Patterns
- MySQL 普通索引、唯一索引、主键索引、全文索引
- 下载代码参考
- 百度地图开发在使用tabhost时候,不能成功定位
- 谭浩强C语言书5.8(2)题
- ORCLE字符集问题
- fotran常用命令
- Python个人学习笔记二
- cocos2dx3.X项目重写(二)新的物理引擎
- Windows 9开发者预览版可下载? 小心是钓鱼网站!
- iOS Xcode设置项之Architectures和Valid Architectures