Java FileInputStream Read writeFile(String path, OutputStream out)

Here you can find the source of writeFile(String path, OutputStream out)

Description

write File

License

GNU General Public License

Declaration

public static void writeFile(String path, OutputStream out) throws IOException 

Method Source Code

//package com.java2s;
/**// w  w  w .ja  v  a2s .co m
 * License
 * 
 * Licensed under the GNU GPL v3
 * http://www.gnu.org/licenses/gpl.html
 * 
 */

import java.io.BufferedInputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import java.io.OutputStream;

public class Main {
    public static void writeFile(String path, OutputStream out) throws IOException {
        File file = new File(path);
        if (file.exists() && !file.isDirectory()) {
            out.write(("HTTP/1.1 200 OK" + "\r\n" + "Content-Type: text/html" + "\r\n" + "Content-Length: "
                    + file.length() + "\r\n" + "\r\n").getBytes());
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
            while (true) {
                int data = in.read();
                if (data == -1)
                    break;
                out.write(data);
            }
            in.close();
        } else {
            String ct = "400 - NOT FIND! \r\n PATH : " + path;
            out.write(("HTTP/1.1 400 NOT FIND" + "\r\n" + "Content-Type: text/html" + "\r\n" + "Content-Length: "
                    + ct.getBytes("iso-8859-1") + "\r\n" + "\r\n").getBytes());
            out.write(ct.getBytes());
        }
        out.flush();
    }
}

Related

  1. readFileUTF8(String file)
  2. readFileUTF_8(String filePath)
  3. writeFile(File file, byte[] buffer, ZipOutputStream zos)
  4. writeFile(InputStream fileInputStream, OutputStream outputStream)
  5. writeFile(OutputStream os, File f)
  6. writeFileToOutputStream(File file, OutputStream output)
  7. writeFileToStream(File file, OutputStream os)
  8. writeFileToZipStream(String path, File f, ZipOutputStream out)