51Testing软件测试论坛

标题: LR中调用 JAVA类 测试上传文件的问题(急!!!) [打印本页]

作者: goopy    时间: 2013-11-5 11:23
标题: LR中调用 JAVA类 测试上传文件的问题(急!!!)
最近在测试上传文件的接口,需要在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));
  }
}
}
作者: goopy    时间: 2013-11-7 11:22
高手出來說下了




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2