首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

下载公用步骤

2012-06-29 
下载公用方法public void toUpload(HttpServletResponse response,String downPath,String strs){ ??try {

下载公用方法
public void toUpload(HttpServletResponse response,String downPath,String strs){
??try {
???String path = downPath+"\"+strs;
???if (!"".equals(path)) {
????File file = new File(path);
????if (file.exists()) {
?????InputStream ins = new FileInputStream(path);
?????BufferedInputStream bins = new BufferedInputStream(ins);// 放到缓冲流里面
?????OutputStream outs = response.getOutputStream();// 获取文件输出IO流
?????BufferedOutputStream bouts = new BufferedOutputStream(outs);
?????response.setContentType("application/x-download");// 设置response内容的类型
?????response.setHeader("Content-disposition",
???????"attachment;filename="
?????????+ URLEncoder.encode(strs, "UTF-8"));// 设置头部信息
?????int bytesRead = 0;
?????byte[] buffer = new byte[8192];
?????// 开始向网络传输文件流
?????while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {
??????bouts.write(buffer, 0, bytesRead);
?????}
?????bouts.flush();// 这里一定要调用flush()方法
?????ins.close();
?????bins.close();
?????outs.close();
?????bouts.close();
????}
???}
??} catch (IOException e) {
???e.printStackTrace();
??}
?}

热点排行