cxf文件上传运行实例

Fileentity.java

package com.yunix.service.entity;

import javax.activation.DataHandler;

/**
 * 文件实体类
 * @author yunix
 * 2013-3-2 上午11:19:22
 */
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;
	}
	
}

 IFileService.java

package com.yunix.service.webservice;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.soap.MTOM;

import com.yunix.service.entity.FileEntity;

/**
 * 文件service接口
 * @author yunix
 * 2013-3-2 上午11:22:22
 */
@WebService
@MTOM
public interface IFileService {

	@WebMethod
	public void uploadFile(@WebParam(name="fileEntity")FileEntity fileEntity);
}

 FileService.java

package com.yunix.service.webservice;

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

import javax.activation.DataHandler;

import com.yunix.service.entity.FileEntity;

/**
 * 文件Service实现
 * @author yunix
 * 2013-3-2 上午11:24:57
 */

public class FileService implements IFileService {

	@Override
	public void uploadFile(FileEntity fileEntity) {
		System.out.println("1");
		DataHandler handler = fileEntity.getFile();
		System.out.println("2");
		try {
			InputStream is = handler.getInputStream();
			OutputStream os = new FileOutputStream(new File("F:/"+fileEntity.getFileName()+"."+fileEntity.getFileType()));
			byte[] b = new byte[100000];
			int bytesRead = 0;
			while((bytesRead = is.read(b)) != -1){
				os.write(b, 0,bytesRead);
			}
			System.out.println("3");
			os.flush();
			os.close();
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

 cxf-servlet.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.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

	<jaxws:endpoint id="file" implementor="com.yunix.service.webservice.FileService" address="/fileService">
	</jaxws:endpoint>
	  
</beans>

 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>cxf</display-name>
	<description>Apache CXF Endpoint</description>
	<listener>           
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
    	<param-name>contextConfigLocation</param-name>
    	<param-value>classpath*:cxf-servlet.xml</param-value>
  	</context-param>
    <servlet>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

 启动并运行项目

http://localhost:8080/service-file/fileService?wsdl

 可查看wsdl

测试代码:

package com.test;

import java.io.File;  

import javax.activation.DataHandler;  
import javax.activation.DataSource;  
import javax.activation.FileDataSource;  
  
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  

import com.yunix.service.entity.FileEntity;
import com.yunix.service.webservice.IFileService;
  
public class Test {  
    public static void main(String[] args) throws Exception {  
          
        String url = "http://localhost:8080/service-file/fileService?wsdl";  
         FileEntity file = new FileEntity();  
         file.setFileName("2012-03-04TestFile");
         file.setFileType("jpg");  
         DataSource source = new FileDataSource(new File("d:\\123456.jpg"));  
         file.setFile(new DataHandler(source));   
  
         JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
           
         factory.setServiceClass(IFileService.class);  
         factory.setAddress(url);  
         IFileService client = (IFileService) factory.create();  
         try {  
             client.uploadFile(file);  
        } catch (Exception e) {  
            System.out.println(e);  
        }          
           
         System.out.println("success");  
    }  
  
}

 

你可能感兴趣的:(文件上传)