您的位置:首页 > Web前端 > JavaScript

jsp实现文件下载的代码(通过文件流方式)

2014-05-04 17:30 811 查看
<%@ page import="java.io.*" %>

<%@ page import="java.net.URLEncoder" %>

<%@page language="java" contentType="application/x-msdownload" pageEncoding="gb2312"%><%

response.reset();

response.setContentType("application/x-download");

String filedownload = "C:/Users/WangYL/Desktop/jin.jpg";

String filedisplay = "jin.jpg";

filedisplay = URLEncoder.encode(filedisplay,"UTF-8");

response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);

OutputStream outp = null;

FileInputStream in = null;

try

{

outp = response.getOutputStream();

in = new FileInputStream(filedownload);

byte[] b = new byte[1024];

int i = 0;

while((i = in.read(b)) > 0)

{

outp.write(b, 0, i);

}

outp.flush();

}

catch(Exception e)

{

System.out.println("Error!");

e.printStackTrace();

}

finally

{

if(in != null)

{

in.close();

in = null;

}

if(outp != null)

{

outp.close();

outp = null;

}

}

%>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: