WebServiceシステム——CXF+SPRINGファイルアップロード


WebServiceシステム——CXF+SPRINGファイルアップロード
要旨:このノートは1つのwebプロジェクトの中で避けられない機能--ファイルのアップロードを実現します.主にFileEntityというfileのパッケージjavaBeanの構築です.テストには2つの方法が使用され、1つは元のwebserviceインタフェースの結果を取得することであり、もう1つはspringを使用してアップロードを実現することである.
 
一:紹介
 
前に構築したspring+webserviceプロジェクトに基づいてファイルアップロードを実現します.
1、サーバー側にfile情報を表すJavaBean:FileEntityを追加する.
2、アップロードファイルのサービスインタフェースを作成する.
3、ファイルをアップロードするサービスインタフェースを実現する.
4、ファイルをアップロードするサービスインタフェースをspringで登録して発行する.
5、新しいwebserviceクライアントプロジェクト(前のノートで作成したクライアントプロジェクトを直接使用できます).
6、クライアントでfileエンティティクラスを作成する:FileEntity(属性名は完全に同じで、単純な点は直接コピーで、パッケージ名も同じ).
7、サービス側の機能と全く同じアップロードファイルインタフェースを作成する(直接コピーする.パッケージ名も同じであることに注意する).
8、springプロファイルを使用して、サーバー側が発行したアップロードファイルのwebserviceを取得する.
9、テスト:
a)元の取得方式でテストする.
b)springで登録したwebserviceテストを使用する.
 
二:具体的な実現手順
 
1、サーバー側にfile情報を表すJavaBean:FileEntityコードを追加する:
package com.chy.ws.entity;

import javax.activation.DataHandler;

public class FileEntity {
	private String fileName;
	private String fileType;
	private DataHandler file;
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public String getFileType() {
		return fileType;
	}
	public void setFileType(String fileType) {
		this.fileType = fileType;
	}
	public DataHandler getFile() {
		return file;
	}
	public void setFile(DataHandler file) {
		this.file = file;
	}
}

2、アップロードファイルのサービスインタフェースを作成する——UploadFileServiceコード:
package com.chy.ws.service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import com.chy.ws.entity.FileEntity;

@WebService
public interface UploadFileService {
	
	@WebMethod
	public void uploadFile(@WebParam(name="fileEntity") FileEntity fileEntity);
}

3、アップロードファイルのサービスインタフェースを実現する——UploadFileServiceImplコード:
package com.chy.ws.service;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.activation.DataHandler;

import com.chy.ws.entity.FileEntity;

public class UploadFileServerImpl implements UploadFileService {

	@Override
	public void uploadFile(FileEntity fileEntity) {
		DataHandler handler = fileEntity.getFile();
		InputStream is = null;
		OutputStream os = null;
		try {
			is = handler.getInputStream();
			os = new FileOutputStream("F:/" + fileEntity.getFileName() + "."
					+ fileEntity.getFileType());
			int n = 0;
			byte[] b = new byte[1024];
			while ((n = is.read(b)) != -1) {
				os.write(b, 0, n);
			}
			os.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (is != null) {
					is.close();
				}
				if (os != null) {
					os.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

4、アップロードファイルのサービスインタフェースをspringで登録して発行する——springプロファイルアプリケーションContext-server.xmlコード:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="    
            http://www.springframework.org/schema/beans     
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
            http://cxf.apache.org/jaxws    
            http://cxf.apache.org/schemas/jaxws.xsd">

	<!-- Import apache CXF bean definition   -->
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

	<!-- services     -->
	<bean id="helloServicesBean" class="com.chy.ws.service.HelloServiceImpl" />
	<bean id="uploadFileServiceBean" class="com.chy.ws.service.UploadFileServerImpl" />

	<!-- CXF   WebServices          -->
	<jaxws:server id="helloService" address="/HelloService" serviceClass="com.chy.ws.service.HelloService">
		<jaxws:serviceBean>
			<ref bean="helloServicesBean" />
		</jaxws:serviceBean>
	</jaxws:server>

	<jaxws:server id="uploadFileService" address="/UploadFileService" serviceClass="com.chy.ws.service.UploadFileService">
		<jaxws:serviceBean>
			<ref bean="uploadFileServiceBean" />
		</jaxws:serviceBean>
	</jaxws:server>
</beans>  

5、クライアント——FileEntityコード:
package com.chy.ws.entity;

import javax.activation.DataHandler;

public class FileEntity {
	private String fileName;
	private String fileType;
	private DataHandler file;
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public String getFileType() {
		return fileType;
	}
	public void setFileType(String fileType) {
		this.fileType = fileType;
	}
	public DataHandler getFile() {
		return file;
	}
	public void setFile(DataHandler file) {
		this.file = file;
	}
}

6、クライアント——UploadFileServiceコード:
package com.chy.ws.service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import com.chy.ws.entity.FileEntity;

@WebService
public interface UploadFileService {
	
	@WebMethod
	public void uploadFile(@WebParam(name="fileEntity") FileEntity fileEntity);
}

7、クライアント——アプリケーションContext-client.xmlコード:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="    
            http://www.springframework.org/schema/beans     
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
            http://cxf.apache.org/jaxws    
            http://cxf.apache.org/schemas/jaxws.xsd">
	<!-- Import apache CXF bean definition -->
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

	<!-- CXF webservices       -->
	<jaxws:client id="helloClient" serviceClass="com.chy.ws.service.HelloService"
		address="http://localhost:8080/webservice_spring_server/services/HelloService">
	</jaxws:client>

	<!--      -->
	<jaxws:client id="uploadFileService" serviceClass="com.chy.ws.service.UploadFileService"
		address="http://localhost:8080/webservice_spring_server/services/UploadFileService">
	</jaxws:client>
</beans>  

8、テスト——UploadClientコード:
 
package com.chy.ws.test;

import java.io.File;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.chy.ws.entity.FileEntity;
import com.chy.ws.service.UploadFileService;

@SuppressWarnings("unused")
public class UploadFileClient {

	/**
	 * use original method to test upload file.
	 * @param the real file path.
	 */
	private static void invokingUploadFile(String filePath){
		FileEntity fileEntity = constructFileEntity(filePath);
		
		//obtain web service
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setAddress("http://localhost:8080/webservice_spring_server/services/UploadFileService");
		factory.setServiceClass(UploadFileService.class);
		
		//upload file
		UploadFileService uploadFileService = (UploadFileService)factory.create();
		uploadFileService.uploadFile(fileEntity);
	}
	
	/**
	 * use the spring application context to test upload file.
	 * @param the real file path.
	 */
	private static void invokingUploadFileBySpring(String filePath){
		FileEntity fileEntity = constructFileEntity(filePath);
		
		//Obtain the spring UploadFileService through the spring application context!
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-client.xml");
		UploadFileService uploadFileService = applicationContext.getBean("uploadFileService", UploadFileService.class);
		try {
			uploadFileService.uploadFile(fileEntity);
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
		System.out.println("Upload file succeed!");
	}
	
	/**
	 * Construct FileEntity.
	 * @param the real file path.
	 * @return FileEntity.
	 */
	private static FileEntity constructFileEntity(String filePath) {
		// construct FileEntity
		FileEntity fileEntity = new FileEntity();
		File file = new File(filePath);
		fileEntity.setFileName(file.getName().substring(0,(file.getName().lastIndexOf("."))));
		fileEntity.setFileType(filePath.substring(filePath.lastIndexOf(".")+1));
		DataSource source = new FileDataSource(file);
		DataHandler handler = new DataHandler(source);
		fileEntity.setFile(handler);
		return fileEntity;
	}
	
	public static void main(String[] args) {
		String filePath = "D:\\text.txt";
		//invokingUploadFile(filePath);
		invokingUploadFileBySpring(filePath);
	}
}

補足:
 
1、注意:サービス側とクライアントのJavaBeanの名前属性はそっくりで、パッケージと一緒にコピーすることが望ましい.
2、完全なプロジェクト図: