使用JUNIT将文件上传到服务器

huangapple 未分类评论47阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年5月29日 15:35:02
  • 转载请务必保留本文链接:https://java.coder-hub.com/62080909.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定