java 如何在swing中使用用户名和密码从tomcat服务器上传,下载文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7608428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:40:36  来源:igfitidea点击:

how to upload, download file from tomcat server with username,password in swing

javahttptomcat

提问by NSR

I want to make a program in swing which connect to tomcat server running locally. with username,password authentication, then user able to upload file in server directory. ie.http://localhost:8080/uploadfiles. from user defined file path , and same as download to the local directory.

我想在swing中制作一个连接到本地运行的tomcat服务器的程序。使用用户名,密码验证,然后用户可以在服务器目录中上传文件。即 http://localhost:8080/uploadfiles。来自用户定义的文件路径,与下载到本地目录相同。

回答by Maurice Perry

Here is one possibility: Download:

这是一种可能性:下载:

    URL url = new URL("http://localhost:8080/uploadfiles");
    HttpURLConnection con = (HttpURLConnection)url.openConnection();
    try {
        con.addRequestProperty("Authorization",
                "Basic " + encode64(username + ":" + password));
        InputStream in = con.getInputStream();
        try {
            OutputStream out = new FileOutputStream(outFile);
            try {
                byte buf[] = new byte[4096];
                for (int n = in.read(buf); n > 0; n = in.read(buf)) {
                    out.write(buf, 0, n);
                }
            } finally {
                out.close();
            }
        } finally {
            in.close();
        }
    } finally {
        con.disconnect();
    }

Upload:

上传:

    URL url = new URL("http://localhost:8080/uploadfiles");
    HttpURLConnection con = (HttpURLConnection)uploadUrl.openConnection();
    try {
        con.setDoOutput(true);
        con.setRequestMethod("POST");
        con.addRequestProperty("Authorization",
                "Basic " + encode64(username + ":" + password));
        OutputStream out = con.getOutputStream();
        try {
            InputStream in = new FileInputStream(inFile);
            try {
                byte buffer[] = new byte[4096];
                for (int n = in.read(buffer); n > 0; n = in.read(buffer)) {
                    out.write(buffer, 0, n);
                }
            } finally {
                in.close();
            }
        } finally {
            out.close();
        }
        int code = con.getResponseCode();
        if (code != HttpURLConnection.HTTP_OK) {
            String msg = con.getResponseMessage();
            throw new IOException("HTTP Error " + code + ": " + msg);
        }
    } finally {
        con.disconnect();
    }

Now, on the server side, you will need to distinguish between GET and POST requests and handle them accordingly. You will need a library to handle uploads, such as apache FileUpload

现在,在服务器端,您需要区分 GET 和 POST 请求并相应地处理它们。您将需要一个库来处理上传,例如 apache FileUpload

Oh, and on the client side, you will need a library that does Base64 encoding such as apache commons codec

哦,在客户端,您将需要一个执行 Base64 编码的库,例如 apache commons 编解码器