java 工程文件_java 文件download简易工程

public class servlet extends HttpServlet{

private ServletConfig servletConfig;

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {

//bianli(req, resp);

// TODO Auto-generated method stub

//resp.getOutputStream().write("你好".getBytes());

//String usernameString=req.getParameter("username");

//resp.getOutputStream().write(usernameString.getBytes());

bianli(req, resp);

//super.doGet(req, resp);

//int a=0;

//a++;

//String method=req.getMethod();

//String aString=servletConfig.getInitParameter("data");

//resp.getOutputStream().write(Integer.toString(a).getBytes());

//resp.getOutputStream().write(aString.getBytes());

}

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(req, resp);

}

@Override

public void init(ServletConfig config) throws ServletException {

// TODO Auto-generated method stub

//super.init();

this.servletConfig=config;

}

public void bianli(HttpServletRequest req, HttpServletResponse resp){

//File file=new File("D:"+File.separator+"test");

File file=new File("D:");

File[]files=file.listFiles();

/*

try {

resp.getOutputStream().write(files[1].getName().getBytes());

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

*/

boolean youmeiyou=false;

String usernameString=req.getParameter("username");

for (int i = 0; i < files.length; i++) {

if (files[i].getName().contains(usernameString)) {

youmeiyou=true;

InputStream in=null;

OutputStream outputStream=null;

int len=0;

byte[]b=new byte[1024];

resp.setHeader("content-disposition", "attachment;filename="+files[i].getName());

try {

outputStream=resp.getOutputStream();

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

try {

in=new FileInputStream(files[i]);

while ((len=in.read(b))>0) {

outputStream.write(b,0,len);

}

}

catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}return;

}

/*if(!files[i].getName().contains(usernameString)){

try {

resp.getOutputStream().write("你搜索的文件不存在".getBytes());

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

*/

}

if (youmeiyou==false) {

try {

resp.getOutputStream().write("你搜索的文件不存在".getBytes());

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值