import
java.io.BufferedInputStream;
import
java.io.ByteArrayOutputStream;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.OutputStream;
import
junit.framework.TestCase;
public
class
Debug
extends
TestCase
{
public
void
test()
throws
Exception
{
//1.在發(fā)送端用BASE64Encoder將二進(jìn)制數(shù)據(jù)編碼成字符串后再發(fā)送;
String srcfile =
"G:/workspace/hzjx_wsdl/WebContent/images/wj.jpg"
;
byte
[] bt_src = readFile(srcfile);
String temp =
new
sun.misc.BASE64Encoder().encodeBuffer(bt_src);
// .....<webservice傳輸 temp 過程>
//2.在接收端用BASE64Decoder對接收到的字符串解碼成二進(jìn)制數(shù)據(jù);再輸出生成圖片;
byte
[] bt_tag =
new
sun.misc.BASE64Decoder().decodeBuffer(temp);
String tagfile =
"G:/abcde.jpg"
;
saveToFile(bt_tag, tagfile);
System.out.println(
"請檢查目標(biāo)文件<"
+tagfile+
">是否與源文件<"
+srcfile+
">的內(nèi)容肯定是相同的!"
);
System.out.println(
"內(nèi)容相同證明Base64編碼與解碼處理過程肯定是正確的!"
);
}
public
static
byte
[] readFile(String filePath)
{
byte
[] arr_result =
null
;
InputStream in =
null
;
ByteArrayOutputStream fout =
new
ByteArrayOutputStream(
8192
);
try
{
in =
new
FileInputStream(filePath);
BufferedInputStream fin =
new
BufferedInputStream(in,
8192
);
byte
[] data =
new
byte
[
8192
];
int
count =
0
;
while
(count >=
0
)
{
count = fin.read(data);
if
(count >
0
)
{
fout.write(data,
0
, count);
}
}
arr_result = fout.toByteArray();
}
catch
(Exception e)
{
e.printStackTrace();
}
finally
{
if
(in !=
null
)
try
{in.close();}
catch
(Exception ex){}
if
(fout !=
null
)
try
{fout.close();}
catch
(Exception ex){}
}
return
arr_result;
}
public
static
void
saveToFile(
byte
[] bt, String filePath)
{
OutputStream os =
null
;
try
{
File savefile =
new
File(filePath);
os =
new
FileOutputStream(savefile);
os.write(bt);
}
catch
(IOException e)
{
e.printStackTrace();
}
finally
{
if
(os !=
null
){
try
{os.close();}
catch
(Exception ex){}}
}
}
}
聯(lián)系客服