Java读取网站需下载的文件

为什么会产生这样一个需求,原因是用户想读取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();  
    } 

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值