Java中下载文件的方法有哪些

  介绍

这期内容当中小编将会给大家带来有关Java中下载文件的方法有哪些,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

<强> 1。以流的方式下载。

public  HttpServletResponse 下载(String 路径,HttpServletResponse 响应),{   ,try  {//才能,路径是指欲下载的文件的路径。   File 才能;File =, new 文件(路径);//,才能取得文件名。   String 才能;filename =, file.getName ();//,才能取得文件的后缀名。   String 才能;ext =, filename.substring (filename.lastIndexOf (“!”), +, 1) .toUpperCase ();   ,//才能,以流的形式下载文件。   InputStream 才能;fis =, new  BufferedInputStream (new  FileInputStream(路径));   ,,byte [], buffer =, new 字节(fis.available ());   fis.read才能(缓冲);   fis.close才能();//才能,清空响应   response.reset才能();//才能,设置响应的头   response.addHeader才能(“Content-Disposition",,“附件;文件名=?+,new 字符串(filename.getBytes ()));   response.addHeader才能(“Content-Length",,,,, +, file.length ());   OutputStream 才能;toClient =, new  BufferedOutputStream (response.getOutputStream ());   response.setContentType才能(“应用程序/octet-stream");   toClient.write才能(缓冲);   toClient.flush才能();   toClient.close才能();   ,}catch  (IOException 交货),{   ex.printStackTrace才能();   ,}   ,return 响应;   以前,}

<强> 2。下载本地文件

public  void  downloadLocal (HttpServletResponse 响应),throws  FileNotFoundException  {   ,//下载本地文件   ,String  fileName =,“Operator.doc" .toString();,//,文件的默认保存名   ,//读到流中   ,InputStream  inStream =, new  FileInputStream (“c:/Operator.doc");//,文件的存放路径   ,//设置输出的格式   ,response.reset ();   ,response.setContentType (“bin");   ,response.addHeader (“Content-Disposition",,“附件;,文件名=\““,+,fileName  +,“\““);   ,//循环取出流中的数据   ,byte [], b =, new 字节[100];   ,int 兰;   ,try  {   while 才能;((len =, inStream.read (b)),祝辞,0)   response.getOutputStream才能().write (b,, 0,, len);   inStream.close才能();   ,}catch  (IOException  e), {   e.printStackTrace才能();   ,}   以前,}

<强> 3。下载网络文件

public  void  downloadNet (HttpServletResponse 响应),throws  MalformedURLException  {//,才能下载网络文件   int 才能;bytesum =, 0;   int 才能;byteread =, 0;   ,   URL 才能;URL =, new  URL (“;windine.blogdriver.com/logo.gif");   ,   try {才能   ,,URLConnection  conn =, url.openConnection ();   ,,InputStream  inStream =, conn.getInputStream ();   ,,FileOutputStream  fs =, new  FileOutputStream (“c:/abc.gif");   ,   ,,byte [], buffer =, new 字节[1204];   ,,int 长度;   ,,while  ((=byteread  inStream.read(缓冲)),!=,1),{   ,,,bytesum  +=, byteread;   ,,,System.out.println (bytesum);   ,,,fs.write(缓冲,0,byteread);   ,,}   ,,},catch  (FileNotFoundException  e), {   ,,e.printStackTrace ();   ,,},catch  (IOException  e), {   ,,e.printStackTrace ();   ,,}   以前,}

<强> 4。支持在线打开的方式

public  void 下载(String  filePath, HttpServletResponse 反应,boolean  isOnLine), throws  Exception  {   File 才能;f =, new 文件(filePath);   if 才能;(! f.exists ()), {   ,,response.sendError(404年,“File  not 发现!“);   ,才能回报;   ,,}   BufferedInputStream 才能;br =, new  BufferedInputStream (new  FileInputStream (f));   ,,byte [], buf =, new 字节[1024];   int 才能;len =, 0;   ,   response.reset才能();,//,非常重要   if 才能;(isOnLine),{,//在线打开方式   ,,URL  u =, new  URL(“文件:///,,+,filePath);   ,,response.setContentType (u.openConnection () .getContentType ());   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

Java中下载文件的方法有哪些