大多數開發者以爲網絡之間進行附件傳輸時候通常想到的是ftp等文件傳輸的形式。其實做爲一名java開發者,小文件之間的網絡傳輸進行base64轉換也是一個不錯的選擇。 java
以下是個人源代碼: web
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流互相流轉
*
* @author wenhao
* 羅文浩
*
*/
public class Base64ConvertByFile {
BASE64Decoder decoder = new BASE64Decoder();
/**
* @Title : 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);
}
}
spring