應用AXIS2框架向服務端上傳文件:
服務端代碼:
public void uploadImageWithByte(byte[] imageByte, int length){
FileOutputStream fos = null;
try
{
// 將上傳的圖像保存在D盤的test1.jpg文件中
fos = new FileOutputStream("c:/test/test1.DAT");
// 開始寫入圖像文件的字節
fos.write(imageByte, 0, length);
fos.close();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (fos != null)
{
try
{
fos.close();
}
catch (Exception e)
{
}
}
}
}
客戶端代碼:
RPCServiceClient serviceClient;
try {
serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference(ClientCommonParameter.NotFoundsUrl);
options.setTo(targetEPR);
// 下面的代碼調用uploadImageWithByte方法上傳圖像文件
/////////////////////////////////////////
// 打開圖像文件,確定圖像文件的大小
java.io.File file = new java.io.File("c:/Upload/test1.DAT");
java.io.FileInputStream fis = null;
fis = new java.io.FileInputStream("c:/Upload/test1.DAT");
// 創建保存要上傳的圖像文件內容的字節數組
byte[] buffer = new byte[(int) file.length()];
// 將圖像文件的內容讀取buffer數組中
int n = 0;
n = fis.read(buffer);
//n爲讀入的字節數
System.out.println("文件長度:" + file.length());
Object[] opAddEntryArgs = new Object[]{ buffer, n };
Class[] classes = new Class[]{ };
QName opAddEntry = new QName(ClientCommonParameter.Qurl,"uploadImageWithByte");
fis.close();
serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes);
// 開始上傳圖像文件,並輸出uploadImageWithByte方法的返回傳
// System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]);
} catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
======================================================================
文件下載客戶端:
RPCServiceClient serviceClient;
try {
serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference(ClientCommonParameter.NotFoundsUrl);
options.setTo(targetEPR);
Object[] opAddEntryArgs = new Object[] {};
Class[] classes = new Class[] {byte[].class};
QName opAddEntry = new QName(ClientCommonParameter.Qurl, "down");
byte[] strArray = (byte[])serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0];
FileOutputStream fos = null;
// 將下載的圖像保存在D盤的test1.jpg文件中
fos = new FileOutputStream("c:/test/bb.DAT");
// 開始寫入圖像文件的字節
fos.write(strArray);
fos.close();
System.out.println("OK");
} catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
服務端:
public byte[] down() throws Exception
{
// 下面的代碼調用uploadImageWithByte方法上傳圖像文件
// 打開圖像文件,確定圖像文件的大小
java.io.File file = new java.io.File("c:/test/test1.DAT");
java.io.FileInputStream fis = new java.io.FileInputStream
("c:/test/test1.DAT");
// 創建保存要上傳的圖像文件內容的字節數組
byte[] buffer = new byte[(int) file.length()];
// 將圖像文件的內容讀取buffer數組中
int n = fis.read(buffer); //n爲讀入的字節數
System.out.println("文件長度:" + file.length());
fis.close();
return buffer;
}