Java读取网站需下载的文件

来源:互联网 发布:淘宝商铺怎么开通花呗 编辑:程序博客网 时间:2024/04/27 11:46

为什么会产生这样一个需求,原因是用户想读取Oracle数据库存储的BLOB字段的值。但是直接在数据层面我又不会操作将其导出.BLOB存储着各种类型的文件。而页面正好有下载需要的连接,后来一想,就读连接把数据写出来。

直接上代码。在D盘放了cross文件,里面存放的是需要读取的文件名,文件名是按行放的,因为读取的时候时按行读的。
然后main方法一运行就OK了。

import java.io.BufferedReader;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.URL;import java.net.URLConnection;public class DownLoadFiles {    public static void main(String[] args){        FileInputStream in = null;        BufferedReader reader=null;        try {            System.out.println("以行为单位读取文件内容,一次读一整行:");            in=new FileInputStream("D://cross.txt");            reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));            String paths = null;            int line = 1;            // 一次读入一行,直到读入null为文件结束                       while ((paths = reader.readLine()) != null) {               String pn = URLEncoder.encode(paths, "utf-8");  //防止有空格,Server returned HTTP response code: 505                try {                    SendGET("http://XXXX:9083/isp/attach/download.do","filePath=/knowledge_manager/"+pn,paths);                } catch (Exception e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                System.out.println("line " + line + ": " + paths);                line++;            }        } catch (IOException e) {            e.printStackTrace();        } finally {            if (in != null) {                try {                    in.close();                } catch (IOException e1) {                }            }            if (reader != null) {                try {                    reader.close();                } catch (IOException e1) {                }            }        }    }    public static void SendGET(String url,String param,String paths) throws Exception{                  try {              //创建url              URL realurl=new URL(url+"?"+param);              //打开连接              URLConnection connection=realurl.openConnection();               // 设置通用的请求属性                       connection.setRequestProperty("accept", "*/*");                       connection.setRequestProperty("connection", "Keep-Alive");                       connection.setRequestProperty("user-agent",                               "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");                       //建立连接                       connection.connect();                       InputStream inStream = connection.getInputStream();                       byte[] data = readInputStream(inStream);                     File imageFile = new File("D:" + File.separator + paths);                       //创建输出流                       FileOutputStream outStream = new FileOutputStream(imageFile);                       //写入数据                       outStream.write(data);                       //关闭输出流                       outStream.close();                                } catch (IOException e) {              e.printStackTrace();             }          }      public static byte[] readInputStream(InputStream inStream) throws Exception{          ByteArrayOutputStream outStream = new ByteArrayOutputStream();          //创建一个Buffer字符串          byte[] buffer = new byte[1024];          //每次读取的字符串长度,如果为-1,代表全部读取完毕          int len = 0;          //使用一个输入流从buffer里把数据读取出来          while( (len=inStream.read(buffer)) != -1 ){              //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度              outStream.write(buffer, 0, len);          }          //关闭输入流          inStream.close();          //把outStream里的数据写入内存          return outStream.toByteArray();      } }
原创粉丝点击