Spring Boot項目整合CXF,實作webservice接口上傳下載下傳大檔案(包含服務端,消費端)
整合 CXF 實作消費端上傳下載下傳大檔案,spring boot 項目。
具體可以見文章尾部的源碼。
服務端:
cxfconfig 是cxf的一下配置檔案。服務暴露的配置。
cxffilewrapper 存放附件屬性的實體類
FileWS為服務端實作接口
Filewsimpl具體的服務端實作方法,包括對用戶端上傳的簡單處理(儲存),用戶端下載下傳檔案,服務端提供的路徑等。
具體依賴jar見源碼。
消費端:
消費端 除 terryDownloadFile(自建用于下載下傳檔案),springboot啟動類外,其餘都是通過wsdl檔案生成的用戶端檔案。
生成方式百度,一般來說jar 生成和cxf生成式有差別的,jar一般生成資料類型簡單的。
wsdl2java -d D:\\ideaWorkSpace\\src\\main\\java -client http://localhost:8762/soap/user?wsdl
檔案上傳,在生成的代碼,FileWS_FileWSImplPort_Client.java中進行了修改
主要代碼如下:
在源碼中注釋掉了,沒來得及改,其實這段代碼是上傳代碼。
System.out.println("Invoking upload...");
com.zyyfw.CxfFileWrapper _upload_file = new CxfFileWrapper();
_upload_file.setFileName("test.csv");
_upload_file.setFileExtension("csv");
DataSource source = new FileDataSource(new File("d:\\test.csv"));
_upload_file.setFile(new DataHandler(source));
boolean _upload__return = port.upload(_upload_file);
System.out.println("upload.result=" + _upload__return);
檔案下載下傳主要代碼在terryDownLoadFile.java中:
public static void main(String args[]) throws java.lang.Exception {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(FileWS.class);
factory.setAddress("http://localhost:8762/soap/user");
FileWS fileWS = factory.create(FileWS.class);
CxfFileWrapper fileWrapper = fileWS.download();
OutputStream os = null;
InputStream is = null;
BufferedOutputStream bos = null;
try
{
is = fileWrapper.getFile().getInputStream();
// 檔案在用戶端的儲存位置
File dest = new File("d:\\download\\" + fileWrapper.getFileName());
os = new FileOutputStream(dest);
bos = new BufferedOutputStream(os);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.flush();
System.out.println("下載下傳完成");
} catch (
IOException e)
{
e.printStackTrace();
} finally
{
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
}
}
if (os != null) {
try {
os.close();
} catch (Exception e) {
}
}
if (is != null) {
try {
is.close();
} catch (Exception e) {
}
}
}
}
主要代碼就這兩端。具體可以詳見源碼。
參考文章:
https://www.cnblogs.com/huiy/p/6052221.html
https://blog.csdn.net/smugaoyi/article/details/78110879
源碼位址:
https://github.com/TorresZheng/webservice-file-transport
聯系方式:微信