在确保安全的自动化中下载ZIP文件

问题描述 投票:0回答:1

我有一个URL(例如https:example.com/pdf),当在GET调用中使用相同的URL并单击Send and Download选项时,将下载我的zip文件。我需要在JAVA中执行相同的过程-放心的自动化。任何人都可以在JAVA代码中提供帮助,因为我是JAVA的新手。

预先感谢

java rest-assured
1个回答
0
投票

你在这里..

import io.restassured.RestAssured;
import io.restassured.filter.log.UrlDecoder;
import io.restassured.response.Response;
import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class DownloadAFileExampleTest {


    public void canDownloadFilesWithRestAssured() throws IOException {

        // By default this is going to be a subfolder in your project, you can change this to an
        // absolute path or resources if you want to. I kept it simple for the example
        String downloadFolder = "downloads";
        File outputPath = new File(downloadFolder);

        // create the folder structure if it does not exist
        outputPath.mkdirs();

        // sometimes we might be bypassing login or need login credentials created by cookies
        // we can create a hashmap of cookies if we need to
        Map<String, String> cookies = new HashMap();
        // e.g. if I needed to inject a session cookie
        //cookies.put("session_id", Secret.SESSION_ID);

        // sometimes our access controls might be via headers so I might need to set those up
        // we can create a hashmap of headers if we need to
        Map<String, String> headers = new HashMap();
        //cookies.put("X-AUTH-CODE", Secret.AUTH_CODE_HEADER);

        // if your url was extracted from a json respose in another message then you might need to decode it first
        // to make sure it is a completely valid URL e.g. doesn't have any \u0026 type values
        String urlToDownload="https://your-api-url";
        urlToDownload = UrlDecoder.urlDecode(urlToDownload, Charset.defaultCharset(), false);

        // the point is, control the filename so you know what you are downloading
        String downloadFileName = "downloadedFile.zip";


        // For the purpose of the test, if the file already exists then I will delete it
        File checkDownloaded = new File(outputPath.getPath(), downloadFileName);
        if(checkDownloaded.exists()) {
            checkDownloaded.delete();
        }

        // get file using RestAssured
        downloadUrlAsFile(cookies, headers, urlToDownload, outputPath, downloadFileName);


        // Added an assert to check if file exists
        checkDownloaded = new File(outputPath.getPath(), downloadFileName);
        Assert.assertTrue(checkDownloaded.exists());


    }

    private void downloadUrlAsFile(final Map<String,String> cookies, final Map<String,String> headers, final String urlToDownload, final File outputPath, final String filename) throws IOException {

        File outputFile = new File(outputPath.getPath(), filename);


        final Response response = RestAssured.given().headers(headers).cookies(cookies).when().get(urlToDownload).andReturn();

        // check if the URL actually exists
        if(response.getStatusCode() == 200){

            // I am choosing to delete the file if it already exists and write it again
            // if it already exists you might choose to return and not overwrite it
            if (outputFile.exists()) {
                outputFile.delete();
            }

            // I might choose to use the mime type of the file to control the file extension
            // here I am just outputting it to the console to demonstrate how to get the type
            System.out.println("Downloaded an " + response.getHeader("Content-Type"));

            // get the contents of the file
            byte[] fileContents = response.getBody().asByteArray();

            // output contents to file
            OutputStream outStream=null;

            try {

                outStream = new FileOutputStream(outputFile);
                outStream.write(fileContents);

            }catch(Exception e){

                System.out.println("Error writing file " + outputFile.getAbsolutePath());

            }finally {

                if(outStream!=null){
                    outStream.close();
                }
            }
        }
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.