获取文件(401 未授权)- Alfresco
Get file (401 unauthorized) - Alfresco
我在 Alfresco 中获得了一个文件,例如 link:http://localhost:8080/share/proxy/alfresco/workspace/SpacesStore/c9d187e8-aec5-4177-9587-a5b924e514cd/exemplo.pdf 和 javascript 上的 XMLHttpRequest,我没有遇到任何问题。但我正在尝试在 JAVA 上执行此操作:
public static byte[] callURL(String myURL) {
System.out.println("Requeted URL:" + myURL);
StringBuilder sb = new StringBuilder();
URLConnection urlConn = null;
InputStreamReader in = null;
try {
URL url = new URL(myURL);
urlConn = url.openConnection();
if (urlConn != null)
urlConn.setReadTimeout(60 * 1000);
if (urlConn != null && urlConn.getInputStream() != null) {
in = new InputStreamReader(urlConn.getInputStream(),
Charset.defaultCharset());
BufferedReader bufferedReader = new BufferedReader(in);
if (bufferedReader != null) {
int cp;
while ((cp = bufferedReader.read()) != -1) {
sb.append((char) cp);
}
bufferedReader.close();
}
}
in.close();
} catch (Exception e) {
throw new RuntimeException("Exception while calling URL:" + myURL, e);
}
String sbString = sb.toString();
System.out.println(sbString);
byte[] b = sbString.getBytes(StandardCharsets.UTF_8);
return b;
}
但这给了我
401 unauthorized
。问题是什么?我需要任何身份验证吗?我该如何解决这个问题?
我解决了!对于可能遇到相同问题的人,请遵循此 tutorial。您知道如何访问内容。
纯文本:
ContentReader reader = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
String content = reader.getContentString();
二进制数据:
ContentReader reader = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
InputStream originalInputStream = reader.getContentInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final int BUF_SIZE = 1 << 8; //1KiB buffer
byte[] buffer = new byte[BUF_SIZE];
int bytesRead = -1;
while((bytesRead = originalInputStream.read(buffer)) > -1) {
outputStream.write(buffer, 0, bytesRead);
}
originalInputStream.close();
byte[] binaryData = outputStream.toByteArray();
我在 Alfresco 中获得了一个文件,例如 link:http://localhost:8080/share/proxy/alfresco/workspace/SpacesStore/c9d187e8-aec5-4177-9587-a5b924e514cd/exemplo.pdf 和 javascript 上的 XMLHttpRequest,我没有遇到任何问题。但我正在尝试在 JAVA 上执行此操作:
public static byte[] callURL(String myURL) {
System.out.println("Requeted URL:" + myURL);
StringBuilder sb = new StringBuilder();
URLConnection urlConn = null;
InputStreamReader in = null;
try {
URL url = new URL(myURL);
urlConn = url.openConnection();
if (urlConn != null)
urlConn.setReadTimeout(60 * 1000);
if (urlConn != null && urlConn.getInputStream() != null) {
in = new InputStreamReader(urlConn.getInputStream(),
Charset.defaultCharset());
BufferedReader bufferedReader = new BufferedReader(in);
if (bufferedReader != null) {
int cp;
while ((cp = bufferedReader.read()) != -1) {
sb.append((char) cp);
}
bufferedReader.close();
}
}
in.close();
} catch (Exception e) {
throw new RuntimeException("Exception while calling URL:" + myURL, e);
}
String sbString = sb.toString();
System.out.println(sbString);
byte[] b = sbString.getBytes(StandardCharsets.UTF_8);
return b;
}
但这给了我
401 unauthorized
。问题是什么?我需要任何身份验证吗?我该如何解决这个问题?
我解决了!对于可能遇到相同问题的人,请遵循此 tutorial。您知道如何访问内容。
纯文本:
ContentReader reader = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
String content = reader.getContentString();
二进制数据:
ContentReader reader = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
InputStream originalInputStream = reader.getContentInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final int BUF_SIZE = 1 << 8; //1KiB buffer
byte[] buffer = new byte[BUF_SIZE];
int bytesRead = -1;
while((bytesRead = originalInputStream.read(buffer)) > -1) {
outputStream.write(buffer, 0, bytesRead);
}
originalInputStream.close();
byte[] binaryData = outputStream.toByteArray();