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;
String senderId = args;
String targetUserId = args;
String filePath = args;
String fileName = args;
String fileType = args;
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("\\", "\\\\"));
//將文件信息發送給服務器
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("\\", "\\\\")).append("\",");
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;
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));
}
}
} 高手出來說下了
页:
[1]