英文:
Upload File to server using JUNIT
问题
@Test
void test() {
try {
String filePath = "C:/Users/Administrator/Desktop/backupv2/imageMgmt/asdf/";
File directory = new File(filePath);
if (!directory.exists()) {
Files.createDirectories(Paths.get(filePath));
directory.mkdir();
}
String val = "space";
File imageValue = new File("C:/Users/Administrator/Desktop/backupv2/" + val + ".gif");
FileInputStream imageInFile = new FileInputStream(imageValue);
byte[] imageByte= new byte[(int) (imageValue.length())];
imageInFile.read(imageByte);
String base64String = Base64.getEncoder().encodeToString(imageByte);
//Path destinationFile = Paths.get("https://172.20.188.63:8553/m/" + val + ".gif");
Path destinationFile = Paths.get(filePath + val + ".gif");
byte[] decoded = Base64.getDecoder().decode(base64String);
System.out.println("Path: " + destinationFile.toString());
Files.write(destinationFile, decoded);
System.out.println("Upload success!");
} catch(Exception e) {
}
}
在 JUNIT 中通过 URL 路径上传文件到服务器是否可能?
在本地,我可以将示例图片上传到不同的目录,但无法直接上传到服务器。
英文:
@Test
void test() {
try {
String filePath = "C:/Users/Administrator/Desktop/backupv2/imageMgmt/asdf/";
File directory = new File(filePath);
if (!directory.exists()) {
Files.createDirectories(Paths.get(filePath));
directory.mkdir();
}
String val = "space";
File imageValue = new File("C:/Users/Administrator/Desktop/backupv2/" + val + ".gif");
FileInputStream imageInFile = new FileInputStream(imageValue);
byte[] imageByte= new byte[(int) (imageValue.length())];
imageInFile.read(imageByte);
String base64String = Base64.getEncoder().encodeToString(imageByte);
//Path destinationFile = Paths.get("https://172.20.188.63:8553/m/" + val + ".gif");
Path destinationFile = Paths.get(filePath + val + ".gif");
byte[] decoded = Base64.getDecoder().decode(base64String);
System.out.println("Path: " + destinationFile.toString());
Files.write(destinationFile, decoded);
System.out.println("Upload success!");
}catch(Exception e) {
}
}
Is it possible to upload a file to server via url path in JUNIT?
Locally i can upload a sample image to a different directory but not directly to a server.
专注分享java语言的经验与见解,让所有开发者获益!
评论