点击链接浏览器下载文件

        // 首先根据请求,找到服务器端文件存储地址,并创建File文件 
        File file = new File(filePath);
 
        filename = new String(filePath.getBytes("iso8859-1"),"UTF-8");
 
        //设置响应头,控制浏览器下载该文件
        response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
        
        //读取要下载的文件,保存到文件输入流
        DataInputStream dataInputStream =  new DataInputStream(new FileInputStream(file));
        
        //创建输出流
        out = response.getOutputStream();
        
        //缓存区
        byte bufferOut[] = new byte[1024];
        int bytes = 0;
        
        //循环将输入流中的内容读取到缓冲区中
        while ((bytes = dataInputStream.read(bufferOut)) != -1) {
            out.write(bufferOut, 0, bytes);
        }
        
        //关闭
        out.close();

你可能感兴趣的:(java)