51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 2021|回复: 1
打印 上一主题 下一主题

[求助] LR中调用 JAVA类 测试上传文件的问题(急!!!)

[复制链接]
  • TA的每日心情
    开心
    2023-2-8 16:18
  • 签到天数: 13 天

    连续签到: 1 天

    [LV.3]测试连长

    跳转到指定楼层
    1#
    发表于 2013-11-5 11:23:25 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    最近在测试上传文件的接口,需要在LR中调用 上传文件的 JAVA类 测试,现在不知道怎么调用,急需高手赐教,谢谢了,开发人员提供的上传文件的 TestFileTrans.java  代码如下:


    package study.socket;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.net.InetSocketAddress;
    import java.net.Socket;
    public class TestFileTrans {
    public static void main(String[] args) {
      if(args.length != 6){
       System.err.println("please input in order: authToken,senderId,targetUserId,filePath,fileName,fileType");
       System.exit(0);
      }
      
      String authToken = args[0];
      String senderId = args[1];
      String targetUserId = args[2];
      String filePath = args[3];
      String fileName = args[4];
      String fileType = args[5];
      
      new ClientSendFile(authToken,senderId,targetUserId,filePath,fileName,fileType).start();
    }

    static class ClientSendFile extends Thread {
      private String authToken;
      private String senderId;
      private String targetUserId;
      private String filePath;
      private String fileName;
      private String fileType;
      
      public ClientSendFile(){};
      
      public ClientSendFile(String _authToken,String _senderId,String _targetUserId,
        String _filePath,String _fileName,String _fileType) {
       authToken = _authToken;
       senderId = _senderId;
       targetUserId = _targetUserId;
       filePath = _filePath;
       fileName = _fileName;
       fileType = _fileType;
      }
      @SuppressWarnings("unchecked")
      @Override
      public void run() {
       File file = new File(filePath);
       if(!file.exists()){
        throw new IllegalArgumentException("file not found by path:"+file.getPath());
       }
       long fileSize = file.length();
       System.out.println(" client send fileSize:" + fileSize);
       
       long time = System.currentTimeMillis();
       Socket s = new Socket();
       try {
        s.connect(new InetSocketAddress("http://122.146.199.154/MSServer/",41000));
        OutputStream os = s.getOutputStream();
       
        System.out.println(file.getPath().replace("\\", "[url=]\\\\[/url]"));
        //將文件信息發送給服務器
        StringBuilder fileMsgBuilder = new StringBuilder();
        fileMsgBuilder.append("{");
        fileMsgBuilder.append("\"").append("authToken").append("\":\"").append(authToken).append("\",");
        fileMsgBuilder.append("\"").append("clientId").append("\":\"").append(senderId).append("\",");
        fileMsgBuilder.append("\"").append("targetUserId").append("\":\"").append(targetUserId).append("\",");
        fileMsgBuilder.append("\"").append("filePath").append("\":\"").append(file.getPath().replace("\\", "[url=]\\\\")).append("\[/url]",");
        fileMsgBuilder.append("\"").append("transFileName").append("\":\"").append(fileName).append("\",");
        fileMsgBuilder.append("\"").append("fileType").append("\":\"").append(fileType).append("\"}");
       
        DataOutputStream dataOutStream = new DataOutputStream(os);
        dataOutStream.writeUTF(fileMsgBuilder.toString());
        dataOutStream.flush();
        System.out.println("file message:"+fileMsgBuilder);
        // 傳輸文件內容
        int data;
        byte[] buf = new byte[2000];
        FileInputStream fins = new FileInputStream(file);
        while (-1 != (data = fins.read(buf))) {
         os.write(buf, 0, data);
         os.flush();
        }
        System.out.println("client send file content end!");
        os.flush();
        dataOutStream.close();
        fins.close();
        os.close();
        s.close();
       }catch (NumberFormatException e) {
        e.printStackTrace();
       } catch (IOException e) {
        e.printStackTrace();
       }
       System.out.println("start time=" + time
         + " end time=" + System.currentTimeMillis()
         + "-----" + Thread.currentThread().getId()
         + "-----" + filePath
         + "-----" + (System.currentTimeMillis() - time));
      }
    }
    }
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2023-2-8 16:18
  • 签到天数: 13 天

    连续签到: 1 天

    [LV.3]测试连长

    2#
     楼主| 发表于 2013-11-7 11:22:05 | 只看该作者
    高手出來說下了
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-9-20 17:55 , Processed in 0.076322 second(s), 27 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表