webservice上传文件

webservice接口实现文件上传

直接上代码:

服务提供方:

接口:

public String WebfileUploud(@WebParam(name = "resume")Resume resume);

实体:

package com.hb.kfcenter.kfFlow.models;

import javax.activation.DataHandler;

/**
 * WebService进行文件上传 : 上传文件对象类
 * @author admin1
 */
public class Resume {
	private String candidateName;   
    private String resumeFileType;   
    private DataHandler resume;  
    public String getCandidateName() {  
        return candidateName;  
    }  
    public void setCandidateName(String candidateName) {  
        this.candidateName = candidateName;  
    }  
    public String getResumeFileType() {  
        return resumeFileType;  
    }  
    public void setResumeFileType(String resumeFileType) {  
        this.resumeFileType = resumeFileType;  
    }  
    public DataHandler getResume() {  
        return resume;  
    }  
    public void setResume(DataHandler resume) {  
        this.resume = resume;  
    }  
	
}

实现:

	// 文件上传到服务器
	public static String WebfileUploud(Resume resume) {
	 String saveFilePath = "图片路径"
		DataHandler handler = resume.getResume();
		String msg = "";
		OutputStream os = null;
		InputStream is = null;
		try {
			is = handler.getInputStream();
			File saveFile = new File(saveFilePath);
			if (!saveFile.getParentFile().exists()) {
				saveFile.getParentFile().mkdirs();
			}
			os = new FileOutputStream(saveFile);
			byte[] b = new byte[100000];
			int bytesRead = 0;
			while ((bytesRead = is.read(b)) != -1) {
				os.write(b, 0, bytesRead);
			}
			os.flush();
			os.close();
			is.close();
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace();
		}
		return msg;
	}

客户端:

package com.hb.kfcenter.kfFlow.ws;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.rmi.RemoteException;

import javax.xml.rpc.ServiceException;

public class Test0722 {
	public static void main(String[] args) throws ServiceException {
		
		
		Resume resume = new Resume();  
        resume.setCandidateName("你好");  
        resume.setResumeFileType("png");  
        byte[] bytes = File2byte(new File("C:/Users/admin1/Desktop/pdf/111.png"));
        resume.setResume(bytes);
        
        
		ExternalWsLocator locater = new ExternalWsLocator();
		// 测试地址
		String url =   "http://localhost:8080/kfDeploy/csp/services/sr/c_hbdx_ci_serRequest";
		locater.setExternalWebserviceImplPortEndpointAddress(url);
		ExternalWebservice ews = locater.getExternalWebserviceImplPort();
		String webfileUploud = null;
		try {
			webfileUploud = ews.createProblemProcessWithFile(getXml(), resume);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		System.out.println("成功-------" + webfileUploud);
	}
    public static byte[] File2byte(File tradeFile){
        byte[] buffer = null;
        try
        {
            FileInputStream fis = new FileInputStream(tradeFile);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1)
            {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        return buffer;
    }
    
    public static String getXml(){
    	StringBuilder xml1 = new StringBuilder();
    	xml1.append(""   );
    	xml1.append(""   );
    	xml1.append("tel"   );
    	xml1.append("070009002"   );
    	xml1.append("何红丽"   );
    	String nbr  = "17771222375";
		xml1.append(""+nbr+""   );
    	xml1.append("17771222361"  );
    	xml1.append(""   );
    	xml1.append(""   );
    	xml1.append("17771222361" );
    	xml1.append(""   );
    	xml1.append(""   );
    	xml1.append("
" ); xml1.append("0星" ); xml1.append("" ); xml1.append("" ); xml1.append("3601" ); xml1.append("" ); xml1.append("test" ); xml1.append("" ); xml1.append("1006" ); xml1.append("" ); xml1.append("" ); xml1.append("51" ); xml1.append("" ); xml1.append("111111111111111" ); xml1.append("" ); xml1.append("" ); xml1.append("" ); xml1.append("" ); xml1.append("" ); xml1.append("" ); xml1.append("" ); xml1.append("" ); xml1.append("" ); xml1.append("" ); xml1.append("" ); xml1.append("" ); xml1.append("
" ); return xml1.toString(); } }

以上文件 删减部分 所有需要自己修改使用

注意一点 客户端调用,是2进制

byte[] bytes = File2byte(new File(“C:/Users/admin1/Desktop/pdf/111.png”));
resume.setResume(bytes);

你可能感兴趣的:(工具插件类,demo)