java 文件download简易工程
来源:互联网 发布:linux下复制粘贴命令 编辑:程序博客网 时间:2024/05/21 20:17
public class servlet extends HttpServlet{private ServletConfig servletConfig;@Overrideprotected 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());}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// TODO Auto-generated method stubdoGet(req, resp);}@Overridepublic 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 blocke.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 blocke1.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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return;}/*if(!files[i].getName().contains(usernameString)){try {resp.getOutputStream().write("你搜索的文件不存在".getBytes());} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}*/}if (youmeiyou==false) {try {resp.getOutputStream().write("你搜索的文件不存在".getBytes());} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
0 0
- java 文件download简易工程
- JAVA简易文件扫描器
- java 文件的上传下载(File Download)
- java简易登录工程mysql连接
- java download
- android文件下载!download!
- android文件下载!download!
- WebView download 文件
- download隐藏文件路径
- download 文件下载
- Download 文件下载
- 文件上传下载 Upload&Download
- download 下载文件
- Java.io实现简易文件下载
- JAVA实现简易的文件存储系统
- Java读取ini文件简易方法
- java dom4j 解析XML文件 简易教程
- 简易记事本(演示java文件io)
- OpenCV - Rotation (Deskewing)
- Codeforces Round #313 (Div. 2)
- ifanr2基于框架实现
- 2.5.3 创建子弹;2.5.4创建子弹Prefab;2.5.5发射子弹;2.6 创建敌人;2.7.1 添加碰撞体
- Memcached深度分析
- java 文件download简易工程
- 学习CSS(6)
- 三百六十行 行行O2O
- poj1995_Raising Modulo Numbers
- XmlNode中的Value值为空的问题
- TypedArray
- 开始写博客了了了了
- hdoj最小公倍数
- java实现CRC16校验码生成