Я пытаюсь загрузить файл Excel с сайта Share Point. Я могу загрузить файл в свою локальную систему из приложения SharePoint. но когда я пытаюсь открыть загруженный файл Excel, он говорит, что файл поврежден, и его невозможно открыть. а также размер файла увеличивался при загрузке.
Я использую код, как показано ниже.
// Fetch file content as InputStream
InputStream inputStream = graphClient
.sites(siteId)
.drives(driveId)
.root()
.itemWithPath(filePath)
.content()
.buildRequest()
.get();
if (inputStream == null) {
throw new Exception("Failed to fetch file content from SharePoint.");
}
// Read InputStream into byte array
byte[] fileBytes = inputStream.readAllBytes();
logger.info("Downloaded " + fileBytes.length + " bytes.");
// Save the byte array as a file
Path destinationPath = Paths.get(localFilePath);
try (FileOutputStream fos = new FileOutputStream(destinationPath.toFile())) {
fos.write(fileBytes);
logger.info("File saved to " + localFilePath);
} catch (IOException e) {
logger.severe("Error saving the file: " + e.getMessage());
throw e;
}
а также я пробовал использовать байты, такие как код belwo.
try {
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadPath.toFile()));
//FileOutputStream outputStream = new FileOutputStream(downloadPath.toFile());
byte[] buffer = new byte[16 * 1024]; // Buffer size (8 KB)
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
}
outputStream.flush();
logger.info("File downloaded successfully to: " + downloadPath.toString());
} catch (IOException e) {
logger.error("Error while saving the file to disk: " + e.getMessage(), e);
}
помогите пожалуйста в этом вопросе, есть ли другой способ скачать файл. Основная проблема: я не могу открывать файлы, такие как Excel и PDF.
Я также попробовал код ниже
try {
BufferedInputStream bis = new BufferedInputStream(inputStream);
BufferedOutputStream bos = new BufferedOutputStream(Files.newOutputStream(downloadPath, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING));
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadPath.toFile()));
byte[] dataBuffer = new byte[1024];
int bytesRead;
while ((bytesRead = bis.read(dataBuffer, 0, 1024)) != -1) {
bos.write(dataBuffer, 0, bytesRead);
}
outputStream.flush();
logger.info("File downloaded successfully to: " + downloadPath.toString());
} catch (IOException e) {
logger.error("Error while saving the file to disk: " + e.getMessage(), e);
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... sharepoint
Столкнулся с проблемой с поврежденным файлом (Excel) после загрузки из SharePoint с использованием Java-кода Graph API ⇐ JAVA
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Есть ли способ проверить, что файл GLB является действительным или поврежденным файлом в С#?
Anonymous » » в форуме C# - 0 Ответы
- 37 Просмотры
-
Последнее сообщение Anonymous
-