81,092
社区成员
发帖
与我相关
我的任务
分享
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.在发送端用BASE64Encoder将二进制数据编码成字符串后再发送;
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对接收到的字符串解码成二进制数据;再输出生成图片;
byte[] bt_tag = new sun.misc.BASE64Decoder().decodeBuffer(temp);
String tagfile = "G:/abcde.jpg";
saveToFile(bt_tag, tagfile);
System.out.println("请检查目标文件<"+tagfile+">是否与源文件<"+srcfile+">的内容肯定是相同的!");
System.out.println("内容相同证明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){}}
}
}
}
public String [][] getReport(String uname,String pwd,String filePath,String fileName){
String [] str=new String[4];
str[0]=uname;
str[1]=pwd;
filePath="G:/workspace/hzjx_wsdl/WebContent/images/wj.jpg";
File file=new File(filePath);
InputStream is=null;
if(file.exists()){
System.out.println("OK!");
try {
is=new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}else{
System.out.println("NO!");
}
byte[] bt =new byte[4096];
try {
is.read(bt,0,4096);
} catch (IOException e) {
e.printStackTrace();
}
String st=new String(bt);
String temp = new sun.misc.BASE64Encoder().encodeBuffer(bt);
str[2]=temp;//这个就是我需要传输的主要内容,图片。
fileName="123123213S";
str[3]=fileName;
return (String[][]) this.setSoap("getReport",str);
}
public static void fileDownload(byte[] bt ,String fileName) {
String nowTime=Long.toString(System.currentTimeMillis());
File savefile=new File("G:/JAVA/"+nowTime+".jpg");
System.out.println(savefile.toString());
try {
OutputStream os=new FileOutputStream (savefile);
os.write(bt);
} catch (IOException e) {
e.printStackTrace();
}
}