大多数开发者觉得网络之间进行附件传输时候一般想到的是ftp等文件传输的形式。其实作为一名java开发者,小文件之间的网络传输进行base64转换也是一个不错的选择。
如下是我的源代码:
package com.gxt.utils;
import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import org.apache.commons.lang.StringUtils; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; /** * base64 IO流互相流转 * * wenhao * 罗文浩 * */ public class Base64ConvertByFile { BASE64Decoder decoder = new BASE64Decoder(); /** * : ioToBase64 * @描述:流转base64 * @作者: 罗文浩 * @参数: 传入参数定义 * @返回值: String 返回类型 * @throws */ public static String ioToBase64() throws IOException { String fileName = "d:/gjj.docx"; // 源文件 String strBase64 = null; InputStream in = null; try { in = new FileInputStream(fileName); byte[] bytes = org.apache.axiom.attachments.utils.IOUtils.getStreamAsByteArray(in); // in.available()返回文件的字节长度 // byte[] bytes = new byte[in.available()]; // 将文件中的内容读入到数组中 // in.read(bytes); strBase64 = new BASE64Encoder().encode(bytes); // 将字节流数组转换为字符串 strBase64 = fileName.substring(fileName.lastIndexOf("."), fileName.length()).split("\\.")[1] + ";" + strBase64; // strBase64 = new String(strBase64); } catch (FileNotFoundException fe) { fe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } finally { in.close(); } return strBase64; } /** * @Title: ioToBase64 * @描述:流转base64 * @作者: 罗文浩 * @参数: 传入参数定义 * @返回值: String 返回类型 * @throws */ public static String ioToBase64(String fileName) throws IOException { String strBase64 = null; InputStream in = null; try { in = new FileInputStream(fileName); byte[] bytes = org.apache.axiom.attachments.utils.IOUtils.getStreamAsByteArray(in); strBase64 = new BASE64Encoder().encode(bytes); // 将字节流数组转换为字符串 strBase64 = fileName.substring(fileName.lastIndexOf("."), fileName.length()).split("\\.")[1] + ";" + strBase64; } catch (FileNotFoundException fe) { fe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } finally { in.close(); } return strBase64; } public static String ioToBase64(byte[] by, String fileName) throws IOException { String strBase64 = null; try { strBase64 = new BASE64Encoder().encode(by); // 将字节流数组转换为字符串 strBase64 = fileName.substring(fileName.lastIndexOf("."), fileName.length()).split("\\.")[1] + ";" + strBase64; } catch (Exception fe) { fe.printStackTrace(); } finally { } return strBase64; } /** * @Title: base64ToIo * @描述:base64转IO流 * @作者: 罗文浩 * @参数: 传入参数定义 * @返回值: void 返回类型 * @throws */ public static InputStream base64ToIo(String filename, String strBase64) throws IOException { // strBase64 = new String(strBase64); strBase64 = strBase64.replaceAll(" ", ""); String base[] = strBase64.split(";"); Resource resource = new ClassPathResource("com/miitgxt/webservices/utils/"); String filePath = resource.getFile().getPath(); FileOutputStream out = null; String string = base[1]; String fileName = filename + "." + base[0]; // 生成的新文件 InputStream is = null; try { // 解码,然后将字节转换为文件 byte[] bytes = new BASE64Decoder().decodeBuffer(string); // 将字符串转换为byte数组 out = new FileOutputStream(filePath + fileName); out.write(bytes); // 解码,然后将字节转换为文件 // Base64解码 is = new ByteArrayInputStream(bytes); } catch (IOException ioe) { ioe.printStackTrace(); } finally { out.close(); //deleteFile(filePath + fileName); } return is; } public static void deleteFile(String path) { File file = null; try { if (StringUtils.trimToNull(path) != null) { file = new File(path); if (file != null) { if (file.isFile()) { file.delete(); } } } } catch (Exception e) { e.printStackTrace(); } finally { } } public static void main(String[] args) throws IOException { // String basestr = Base64ConvertByFile.ioToBase64(); // Base64ConvertByFile.base64ToIo("11111111111111", basestr); String fileName = "d:/ddd.doc"; String strBase64 = null; strBase64 = fileName.substring(fileName.lastIndexOf("."), fileName.length()).split("\\.")[1] + ";"; System.out.println(strBase64); } }