Java URL Connection getOutputStream(URL url)

Here you can find the source of getOutputStream(URL url)

Description

get Output Stream

License

Open Source License

Declaration

static public OutputStream getOutputStream(URL url) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * //from   w  w w  .j  av a2s .  c  o m
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

public class Main {
    static public OutputStream getOutputStream(URL url) throws IOException {
        if (isFile(url)) {
            File file = new File(url.getPath());
            if (!file.exists()) {
                File parent = file.getParentFile();
                if (parent != null && !parent.exists())
                    parent.mkdirs();
            }
            return new FileOutputStream(file);
        }
        // note that code below does not work for File URLs - "by design" Java 
        // does not support creating output streams on file URLs. Code below should work 
        // for HTTP URLs; no idea as to the other types of URLs
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        return connection.getOutputStream();
    }

    static public boolean isFile(URL url) {
        return ("file".equals(url.getProtocol())); //$NON-NLS-1$
    }

    static public boolean exists(URL url) {
        if (isFile(url)) {
            File file = new File(url.getPath());
            return file.exists();
        }
        return true;
    }
}

Related

  1. getLastModified(URL url)
  2. getLastModified(URLConnection connection)
  3. getLength(String htmlUrl)
  4. getLines(final URL url, final int linesToRead)
  5. getOutputStream(final URL outputURL)
  6. getPage(String url)
  7. getPageContent(String inputUrlString)
  8. getRealURL(URL url)
  9. getRemoteTimestamp(final String url)