加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

接口的客户端测试代码

发布时间:2020-12-17 02:55:22 所属栏目:安全 来源:网络整理
导读:接口的客户端测试代码,含FTP操作和WebService客户端操作。用来提供测试。 备忘。 ---------- package test; import sun.net.ftp.FtpClient; import java.io.RandomAccessFile; import java.io.DataOutputStream; import sun.net.TelnetOutputStre

接口的客户端测试代码,含FTP操作和WebService客户端操作。用来提供测试。

备忘。

----------

package test;

import sun.net.ftp.FtpClient;
import java.io.RandomAccessFile;
import java.io.DataOutputStream;
import sun.net.TelnetOutputStream;

public class ClientTest {
? public ClientTest() {
? }

? public static void save() {
??? String ftpServerAddr = "11.165.21.209";
??? String ftpUserName = "user";
??? String ftpPassword = "password";

??? FtpClient ftp = null;
??? String fileName = "C:/测试.doc";???????????? //要上传的文件(含路径)
??? String newFileName = "new测试.doc"; //上传后的新的文件名字

??? TelnetOutputStream touts = null;
??? DataOutputStream douts = null;
??? int readInt;

??? //链接FTP服务
??? try {
????? ftp = new FtpClient(ftpServerAddr);
????? ftp.login(ftpUserName,ftpPassword);
????? ftp.binary();
??? }
??? catch (Exception ex) {
????? System.out.println("用户或者密码可能不对,无权限与主机:" + ftpServerAddr + "连接!");
??? }

??? //传输文件
??? try {
????? ftp.cd("oa"); //进入指定的FTP服务的OA目录
????? RandomAccessFile sendFile = new RandomAccessFile(fileName,"r");
????? sendFile.seek(0);
????? touts = ftp.put(newFileName);????? //FTP上传后文件的名字
????? douts = new DataOutputStream(touts);
????? while (sendFile.getFilePointer() < sendFile.length()) {
??????? readInt = sendFile.read();
??????? douts.write(readInt);
????? }
??? }
??? catch (Exception e) {
????? System.out.println("FTP文件传输异常!");
????? return;
??? }
??? finally {
????? //关闭链接,释放资源
????? try {
??????? if (douts != null) {
????????? douts.close();
??????? }
??????? if (touts != null) {
????????? touts.close();
??????? }
??????? if (ftp.serverIsOpen()) {
????????? ftp.closeServer();
??????? }
??????? if (ftp != null) {
????????? ftp = null;
??????? }
????? }
????? catch (Exception ex) {
??????? return;
????? }
??? }

??? //调用WebService服务保存数据信息
??? try {
????? OAFileService service = new OAFileServiceLocator();
????? OAFile aOAFile = service.getOAFile();

????? if (aOAFile.saveOAFile("2006",
???????????????????????????? "测试.doc",//原文件名
???????????????????????????? //"new测试.doc",? //上传后的新文件名字
???????????????????????????? "文件种类",
???????????????????????????? "文号",
???????????????????????????? "文件标题",
???????????????????????????? "单位名称",
???????????????????????????? "2006-08-26",
???????????????????????????? "主题词",
???????????????????????????? "2006",
???????????????????????????? "制文单位",
???????????????????????????? 100,
???????????????????????????? "备注内容")) {
??????? System.out.println("成功:" + aOAFile.getSysMsg());
????? }
????? else {
??????? System.out.println("失败:" + aOAFile.getSysMsg());
????? }
??? }
??? catch (Exception e) {
????? System.err.println("Execution failed. Exception: " + e);
??? }
? }

? public static void main(String[] args) {??? save();? }}?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读