Java 文件批量下载

来源:互联网 发布:阿里云服务器 翻墙 编辑:程序博客网 时间:2024/05/21 17:55
转载:http://blog.csdn.net/liu19890121liu/article/details/39229389?locationNum=3ps:批量下载(无弹框,直接保存)import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;public class FileDownload {    public static void main(String[] args) {        //需要压缩的文件--包括文件地址和文件名        String []path ={"D:\\1.txt",        "D:\\2.txt"};        // 要生成的压缩文件地址和文件名称        String desPath = "C:\\Users\\Administrator\\Desktop\\DownLoad.zip";        File zipFile = new File(desPath);        ZipOutputStream zipStream = null;        FileInputStream zipSource = null;        BufferedInputStream bufferStream = null;        try {            //构造最终压缩包的输出流            zipStream = new ZipOutputStream(new FileOutputStream(zipFile));            for(int i =0;i<path.length;i++){                File file = new File(path[i]);                //将需要压缩的文件格式化为输入流                zipSource = new FileInputStream(file);                //压缩条目不是具体独立的文件,而是压缩包文件列表中的列表项,称为条目,就像索引一样                ZipEntry zipEntry = new ZipEntry(file.getName());                //定位该压缩条目位置,开始写入文件到压缩包中                zipStream.putNextEntry(zipEntry);                //输入缓冲流                bufferStream = new BufferedInputStream(zipSource, 1024 * 10);                int read = 0;                //创建读写缓冲区                byte[] buf = new byte[1024 * 10];                while((read = bufferStream.read(buf, 0, 1024 * 10)) != -1)                {                    zipStream.write(buf, 0, read);                }            }        } catch (Exception e) {            e.printStackTrace();        } finally {            //关闭流            try {                if(null != bufferStream) bufferStream.close();                if(null != zipStream) zipStream.close();                if(null != zipSource) zipSource.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }}转载:http://blog.csdn.net/zhxtpray/article/details/38942463ps:单个文件下载(有弹框)    public void download(HttpServletResponse response) {              String filePath = this.queueService.getCsvFilePathById(id);//所要下载的文件路径,从数据库中查询得到,当然也可以直接写文件路径,如:C:\\Users\\Administrator\\Desktop\\csv\\号码_utf8_100.csv              try {                  File file = new File(filePath);                  String fileName = filePath.substring(filePath.lastIndexOf(File.separator)+1);//得到文件名                  fileName = new String(fileName.getBytes("UTF-8"),"ISO8859-1");//把文件名按UTF-8取出并按ISO8859-1编码,保证弹出窗口中的文件名中文不乱码,中文不要太多,最多支持17个中文,因为header有150个字节限制。                response.setContentType("application/octet-stream");//告诉浏览器输出内容为流;设置了头文件才会有弹框;                  response.addHeader("Content-Disposition", "attachment;filename="+fileName);//Content-Disposition中指定的类型是文件的扩展名,并且弹出的下载对话框中的文件类型图片是按照文件的扩展名显示的,点保存后,文件以filename的值命名,保存类型以Content中设置的为准。注意:在设置Content-Disposition头字段之前,一定要设置Content-Type头字段。                  String len = String.valueOf(file.length());                  response.setHeader("Content-Length", len);//设置内容长度                  OutputStream out = response.getOutputStream();                  FileInputStream in = new FileInputStream(file);                  byte[] b = new byte[1024];                  int n;                  while((n=in.read(b))!=-1){                      out.write(b, 0, n);                  }                  in.close();                  out.close();              } catch (FileNotFoundException e) {                  e.printStackTrace();              } catch (IOException e) {                  e.printStackTrace();              }          } 
1 0
原创粉丝点击