TA的每日心情 | 开心 2023-2-8 16:18 |
---|
签到天数: 13 天 连续签到: 1 天 [LV.3]测试连长
|
最近在测试上传文件的接口,需要在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));
}
}
} |
|