package com.fxt.model;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.axis.encoding.Base64;
public class UtilFile {
public static String getFileByteString(File file) throws Exception{
InputStream in = new FileInputStream(file);
long length = file.length();
byte[] bytes = new byte[(int)length];
int offset = 0;
int numRead = 0;
while(offset < bytes.length - offset && (numRead =in.read(bytes, offset, bytes.length - offset)) >=0){
offset += numRead;
}
if(offset < bytes.length){
System.out.println("不能完全读取文件__"+file.getName());
}
in.close();
String encodedFileString = Base64.encode(bytes);
return encodedFileString;
}
}
package com.fxt.model;
import java.io.File;
import java.io.FileOutputStream;
import org.apache.axis.encoding.Base64;
public class UntilFileClient {
public static void main(String[] args) throws Exception {
File f = new File("D:\\4.png");
String str = UtilFile.getFileByteString(f);
byte[] bytes = Base64.decode(str);
// 写入新文件
FileOutputStream out = new FileOutputStream("D:\\"+"wj2.jpg");
out.write(bytes);
out.flush();
out.close();
}
}