Accessing File Over A Network Using Service Account in Java

£可爱£侵袭症+ 提交于 2019-12-09 23:13:14

问题


I am trying to read a file from a network location using Java.The problem is that the access to that network location requires specific credentials.

Is there any way to specify these credentials in Java and then access teh file over the network.

I am trying to do this from my web application which is running on Weblogic

Thanks, Abhishek


回答1:


Here is a sample code.

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();
    }

To make a query string :

//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();
    }


来源:https://stackoverflow.com/questions/21329846/accessing-file-over-a-network-using-service-account-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!