Java Encode encodeFile(String filePath)

Here you can find the source of encodeFile(String filePath)

Description

This webService encodes the files content in Base64 format Usage: For uploading an ISVP to a remote node in the cluster, the isvp file has to be encoded and uploaded.

License

Open Source License

Parameter

Parameter Description
filePath The file to encode.

Return

String Base64 encoded content of the file

Declaration

public static String encodeFile(String filePath) 

Method Source Code

//package com.java2s;
/**//  w  ww  . ja  v  a 2  s  . com
 * Copyright 2008, 2009 Mark Hooijkaas This file is part of the Caas tool. The Caas tool is free software: you can redistribute it
 * and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version
 * 3 of the License, or (at your option) any later version. The Caas tool is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details. You should have received a copy of the GNU General Public License along with the Caas
 * tool. If not, see <http://www.gnu.org/licenses/>.
 */

import sun.misc.BASE64Encoder;

import java.io.DataInputStream;

import java.io.FileInputStream;

import java.io.IOException;

public class Main {
    /**
     * This webService encodes the files content in Base64 format Usage: For uploading an ISVP to a remote node in the cluster,
     * the isvp file has to be encoded and uploaded. This webService encodes the isvp content
     * 
     * @param filePath The file to encode.
     * @return String Base64 encoded content of the file
     */
    public static String encodeFile(String filePath) {
        FileInputStream fin = null;

        try {
            fin = new FileInputStream(filePath);

            byte[] fileContent = new byte[fin.available()];
            DataInputStream din = new DataInputStream(fin);
            din.readFully(fileContent);

            BASE64Encoder encoder = new BASE64Encoder();

            return new String(encoder.encode(fileContent));

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (fin != null)
                try {
                    fin.close();
                } catch (IOException e) {
                }
        }
    }
}

Related

  1. encodeBackslashAhead(char c, char next, Appendable buffer)
  2. encodeCodePoolData(InputStream input)
  3. encodedInputStreamReader(InputStream stream, String encoding)
  4. encodedStringToProperties(String theEncodedPropertyString)
  5. encodeEmail(String s)
  6. encodeForFilter(final String filterString)
  7. encodeForString(String str)
  8. encodeGoomojiSubject(String subject)
  9. encodeHeaderValue(String headerName, String headerValue)