java-IO流/web下载

public static void mkdirs(String path) {
  File file = new File(path);
  if (!file.exists()) {
   file.mkdirs();
  }
 }
文件夹是否存在

public static void download(String urlString, String filename) throws Exception {
        URL url = new URL(urlString);   
        URLConnection con = url.openConnection();
        InputStream is = con.getInputStream();
        byte[] bs = new byte[1024];   
        int len;   
        OutputStream os = new FileOutputStream(filename);   
        while ((len = is.read(bs)) != -1) {   
          os.write(bs, 0, len);   
        }
        os.close();   
        is.close();   
}
远程下载文件到本地
// oracle.sql.Clob类型转换成String类型
    public static String ClobToString(Clob clob) {
        String reString = "";
        Reader is = null;
        try {
            is = clob.getCharacterStream();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 得到流
        BufferedReader br = new BufferedReader(is);
        String s = null;
        try {
            s = br.readLine();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        StringBuffer sb = new StringBuffer();
        while (s != null) {
            // 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
            sb.append(s);
            try {
                s = br.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        reString = sb.toString();
        return reString;
    }
Clob类型转换成String类型
public static File BlobToStream(Blob blob, Map<String, String> inmap)
            throws IllegalAccessException {
        InputStream is = null;
        FileOutputStream fileOutputStream = null;
        String fileName = inmap.get("FILENAME").toString();

        String appDirPath = inmap.get("URLPATH");
        try {
            if (blob != null) {
                is = blob.getBinaryStream();
                ReaderConfigFile.mkdirs(appDirPath);
                File file = new File(appDirPath, fileName);
                if (!file.exists()) {
                    fileOutputStream = new FileOutputStream(file);
                    byte[] buf = new byte[1024];
                    int ch = -1;
                    while ((ch = is.read(buf)) != -1) {
                        fileOutputStream.write(buf, 0, ch);
                    }
                    fileOutputStream.flush();
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {// 关闭资源
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
                if (is != null) {
                    is.close();
                    is = null;
                }
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
        return null;
    }
Blob类型转换成流

posted @ 2013-10-21 15:53  琦琦狐  阅读(252)  评论(0编辑  收藏  举报