outfile.java 3.3 KB
package com.servlet.bksh.DMC;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class outfile extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5716236437264937310L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		
		//得到要下载的文件名称
		String filename=request.getParameter("filename");
		//文件存放的路径,合成绝对路径
		String dir = this.getServletContext().getRealPath("/");
		String filepath=dir+"bksh"+"\\"+filename;
		//得到这个文件的对象
		File file=new File(filepath);
		
		//response的编码方式为.doc下载
		response.setContentType("application/msword");
		//写明要下载的文件的大小
        response.setContentLength((int)file.length());
        //文件名
        response.setHeader("Content-Disposition","attachement;filename=" + new String(filename.getBytes(), "UTF-8"));
		
		//独处文件的IO流
		FileInputStream fis=new FileInputStream(file);
        @SuppressWarnings("resource")
		BufferedInputStream buff=new BufferedInputStream(fis);
		
        byte [] b=new byte[1024];//相当读文件的缓存
        long k=0;//该值用于计算当前实际下载了多少字节
        
        //response对象得到输出流
        OutputStream myout=response.getOutputStream();
        
        //开始循环下载

        while(k<file.length()){

            int j=buff.read(b,0,1024);
            k+=j;

            //将b中的数据写到客户端的内存
            myout.write(b,0,j);

        }

        //将写入到客户端的内存的数据,刷新到磁盘
        myout.flush();
	}

}


/*
 * //request.setCharacterEncoding("utf-8");
 * //response.setCharacterEncoding("GBK"); String
 * filename=request.getParameter("filename");
 * 
 * response.setContentType("image/jpeg");
 * //response.setHeader("Content-Type","application/x-excel");
 * response.setHeader("Content-Disposition", "attachment; filename=" +
 * filename);
 * 
 * 
 * String dir = this.getServletContext().getRealPath("/"); String
 * filepath=dir+"bksh"+"\\"+filename;
 * 
 * File file=new File(filepath); response.setContentLength((int)file.length());
 * InputStream fis=new BufferedInputStream(new FileInputStream(file)); byte[]
 * buffer = new byte[fis.available()]; fis.read(buffer); fis.close();
 * OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
 * toClient.write(buffer); toClient.flush(); toClient.close(); //OutputStream
 * ops=response.getOutputStream(); //int aRead = 0; //while((aRead = fis.read())
 * != -1 & fis != null) //{ //// System.out.println(aRead); // ops.write(aRead);
 * //} //ops.flush(); System.out.println(filepath);
 */