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

在Java中使用服务帐户通过网络访问文件,java,jakarta-ee,web,weblogic-10.x,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();

我正在尝试使用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();
    }
试试看{
URL=新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);
/*
*对于请求头
*/
httpUrlConnection.setRequestProperty(“接受”,
“text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8”);
httpUrlConnection.setRequestProperty(“接受字符集”,
“ISO-8859-1,utf-8;q=0.7,*;q=0.3”);
setRequestProperty(“接受编码”、“gzip、deflate、sdch”);
setRequestProperty(“接受语言”,“en-US,en;q=0.8”);
setRequestProperty(“连接”,“保持活动”);
/*
*用于添加请求参数
*/
Map params=新的HashMap();
参数put(“用户名”、“Visruth”);
参数put(“密码”、“密码”);
OutputStream os=httpUrlConnection.getOutputStream();
BufferedWriter=新的BufferedWriter(
新的OutputStreamWriter(操作系统,“UTF-8”);
write(getQuery(params));
writer.flush();
writer.close();
os.close();
httpUrlConnection.connect();
//写一个文件,像这样
InputStream=httpUrlConnection.getInputStream();
FileOutputStream fos=新的FileOutputStream(“/home/visruth/Desktop/photo2.jpg”);
int i=0;
字节[]b=新字节[1024];
而((i=is.read(b))!=-1){
fos.写入(b,0,i);
}
}捕获(格式错误){
e、 printStackTrace();
}捕获(协议例外e){
e、 printStackTrace();
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(IOE异常){
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();
    }
//如果任何参数有多个值,请对此方法进行必要的更改,例如:usernames=“Visruth”、usernames=“Visruth CV”等。。
私有静态字符串getQuery(映射参数)
抛出不支持的DencodingException{
StringBuilder结果=新建StringBuilder();
布尔值优先=真;
for(条目对:params.entrySet()){
如果(第一){
第一个=假;
}否则{
结果。追加(&);
}
append(URLEncoder.encode(pair.getKey(),“UTF-8”);
结果。追加(“=”);
append(URLEncoder.encode(pair.getValue().toString(),“UTF-8”);
}
返回result.toString();
}