java - 在 Java 中使用服务帐户通过网络访问文件

标签 java jakarta-ee web weblogic-10.x

我正在尝试使用 Java 从网络位置读取文件。问题是访问该网络位置需要特定的凭据。

有没有办法在 Java 中指定这些凭据,然后通过网络访问该文件。

我正在尝试从在 Weblogic 上运行的 Web 应用程序执行此操作

谢谢, 阿布舍克

最佳答案

这是一个示例代码。

try {
        URL url  = new URL("https://www.visruthcv.appspot.com");
        URLConnection con = url.openConnection();

        HttpURLConnection httpUrlConnection = (HttpURLConnection) con;
        httpUrlConnection.setReadTimeout(10000);
        httpUrlConnection.setConnectTimeout(15000);
        httpUrlConnection.setRequestMethod("POST");
        httpUrlConnection.setDoInput(true);
        httpUrlConnection.setDoOutput(true);

        /*
         * for request headers
         */
        httpUrlConnection.setRequestProperty("Accept",
                "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        httpUrlConnection.setRequestProperty("Accept-Charset",
                "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
        httpUrlConnection.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");
        httpUrlConnection.setRequestProperty("Accept-Language", "en-US,en;q=0.8");
        httpUrlConnection.setRequestProperty("Connection", "keep-alive");


        /*
         * for adding request parameters
         */
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("username", "Visruth");
        params.put("password", "passwd");

        OutputStream os = httpUrlConnection.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(getQuery(params));
        writer.flush();
        writer.close();
        os.close();

        httpUrlConnection.connect();

        // To write to a file, something like this
        InputStream is = httpUrlConnection.getInputStream();
        FileOutputStream fos = new FileOutputStream("/home/visruth/Desktop/photo2.jpg");
        int i = 0;
        byte[] b = new byte[1024];

        while((i = is.read(b)) != -1) {
            fos.write(b, 0, i);
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

创建查询字符串:

//make needful changes for this method if any parameter has multiple values, eg: usernames = "Visruth", usernames = "visruth CV" and etc..
private static String getQuery(Map<String, Object> params)
            throws UnsupportedEncodingException {
        StringBuilder result = new StringBuilder();
        boolean first = true;

        for (Entry<String, Object> pair : params.entrySet() ) {


            if (first) {
                first = false;
            } else {
                result.append("&");
            }


            result.append(URLEncoder.encode(pair.getKey(), "UTF-8"));
            result.append("=");
            result.append(URLEncoder.encode(pair.getValue().toString(), "UTF-8"));
        }

        return result.toString();
    }

关于java - 在 Java 中使用服务帐户通过网络访问文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21329846/

相关文章:

c - 如何使用 C 从 www 获取页面的 HTML 代码?

java - 在网页上实现数学概念

java - 使用 Jackson 将 Json 映射到 Java POJO

java - 我可以在 NetBeansIDE 32 位上运行在 NetBeansIDE 64 位中开发的应用程序吗?

java - 使用带有 OpenID Connect 提供程序的 spring-security-oauth2 客户端时如何访问 "id_token"和 "refresh_token"?

java - ThreadLocal 用于管理 session 数据

java - 使用 EJB 客户端 jar 进行远程 EJB 访问

java - Glassfish V3.0 使用 primefaces 组件时出现奇怪的部署错误

java.net.ConnectException : after adding weblogic-application. xml

html - 网站在大多数浏览器上无法正确缩放,大多数元素都放错了位置