Example usage for java.io FileOutputStream write

List of usage examples for java.io FileOutputStream write

Introduction

In this page you can find the example usage for java.io FileOutputStream write.

Prototype

public void write(byte b[], int off, int len) throws IOException 

Source Link

Document

Writes len bytes from the specified byte array starting at offset off to this file output stream.

Usage

From source file:cn.newtouch.util.HttpClientUtil.java

/**
 * //from   w  w  w  .  j a  v a  2  s  .  c om
 * 
 * @param fileUrl
 *            url
 * @param path
 *            ??
 * @param newFilename
 *            ????null??? ??
 * @return ?
 */
public static void downloadFile(String fileUrl, String path, String newFilename) throws Exception {
    if (StringUtils.isEmpty(fileUrl)) {
        throw new Exception("??");
    }
    if (StringUtils.isEmpty(newFilename)) {
        newFilename = fileUrl.substring(fileUrl.lastIndexOf("/"));
    }
    HttpClient client = new HttpClient();
    GetMethod httpGet = new GetMethod(fileUrl);
    try {
        client.executeMethod(httpGet);

        InputStream in = httpGet.getResponseBodyAsStream();
        File outFile = new File(path + File.separator + newFilename);
        if (outFile.exists()) {
            throw new Exception("?");
        }
        FileOutputStream out = new FileOutputStream(outFile);

        byte[] b = new byte[BUFFER];
        int len = 0;
        while ((len = in.read(b)) != -1) {
            out.write(b, 0, len);
        }
        in.close();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("" + e.getMessage());
    } finally {
        httpGet.releaseConnection();
    }
}