下载

 

11222

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.io.OutputStream"%>
<%@page import="java.io.File"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.net.URL"%>
<%@page import="java.net.URLEncoder"%>

<%
    request.setCharacterEncoding("utf-8");
    String filename = request.getParameter("fileName");
    filename = new String(filename.getBytes("iso-8859-1"),"utf-8");
    //得到向客服端输出的输出流
    OutputStream outputStream = response.getOutputStream();
    //输出文件用的字节数组,每次向输出流发送600个字节
    byte[] b = new byte[600];
    //要下载的文件
    File fileload = new File(request.getRealPath(File.separator+"upload"),filename);        
    //客服端使用保存文件的对话框
    response.setHeader("Content-disposition", "attachment;filename="+URLEncoder.encode(filename,"utf-8"));
    //通知客服文件的MIME类型
    response.setContentType("application/x-msdownload");
    //通知客服文件的长度
    long fileLength = fileload.length();
    String length = String.valueOf(fileLength);
    response.setHeader("Content_length", length);
    //读取文件,并发送给客服端下载
    //InputStreamReader inputStream = new InputStreamReader(new FileInputStream(fileload), "utf-8");
    FileInputStream inputStream = new FileInputStream(fileload);
    int n = 0;
    
    while((n=inputStream.read(b))!=-1){
        System.out.println(n);
        outputStream.write(b,0,n);
    }
    outputStream.flush();
    outputStream.close();
    out.clear();  
    out = pageContext.pushBody();
%>

 

posted @ 2015-02-07 17:24  江湖一笑  阅读(163)  评论(0编辑  收藏  举报