Here you can find the source of encodeFile(String filePath)
Parameter | Description |
---|---|
filePath | The file to encode. |
public static String encodeFile(String filePath)
//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) { } } } }