java 文件下载

来源:互联网 发布:京东商城标题优化 编辑:程序博客网 时间:2024/05/08 15:48

下列代码 实现本地文件的下载功能

 

  String fileName = "Operator.doc".toString();  //文件的默认保存名
  //读到流中
  InputStream inStream=new FileInputStream("c:/Operator.doc");//文件的存放路径
  //设置输出的格式
  response.reset();
  response.setContentType("bin");
  response.addHeader("Content-Disposition","attachment; filename=/"" + fileName + "/"");
  //循环取出流中的数据
  byte[] b = new byte[100];
  int len;
  while((len=inStream.read(b)) >0)
  response.getOutputStream().write(b,0,len); 
  inStream.close();

--------------------------------------------------------

下列代码 实现网络文件的下载功能

int bytesum=0;

int byteread=0;

URL url = new URL("windine.blogdriver.com/logo.gif");

 URLConnection conn = url.openConnection();

 InputStream inStream = conn.getInputStream();

 FileOutputStream fs=new FileOutputStream( "c:/abc.gif");

  byte[]  buffer =new  byte[1204];

   int length;

    while ((byteread=inStream.read(buffer))!=-1)

    {

       bytesum+=byteread;

       System.out.println(bytesum);

       fs.write(buffer,0,byteread);

     }

 
原创粉丝点击