简体   繁体   English

Java 从 Apache IOUtils 转换的字符串中取回 InputStream

[英]Java Get back InputStream from String Converted by Apache IOUtils

I am using library Sharepoint-Java-API to download Files from Sharepoint.我正在使用库Sharepoint-Java-API从 Sharepoint 下载文件。
I am able to download file but got as String format which is converted using org.apache.commons.io.IOUtils as我能够下载文件,但得到了使用org.apache.commons.io.IOUtils转换的字符串格式

public static String get(Pair<String, String> token, String url) {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    try {
        HttpGet getRequest = new HttpGet(url);
        getRequest.addHeader("Cookie", token.getLeft() + ";" + token.getRight());
        getRequest.addHeader("accept", "application/json;odata=verbose");
        HttpResponse response = httpClient.execute(getRequest);
        if (response.getStatusLine().getStatusCode() == 200) {
            return IOUtils.toString(response.getEntity().getContent(), "utf-8");
        } else {
            System.err.println("Failed : HTTP error code : " + response.getStatusLine().getStatusCode() + ", " + url);
            return null;
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            httpClient.close();
        } catch (IOException ex) {
            Logger.getLogger(SPOnlineMFT.class).error(ex);
        }
    }
    return null;
}

Now I am trying to convert it to InputStream and save it local file system for testing现在我正在尝试将其转换为InputStream并将其保存在本地文件系统中以进行测试

Caller:-呼叫者:-

String content = SPOnline.get(token, domain, url );
if (content != null) {
    File targetFile = new File("D:\\Rivian\\SharePoint\\TempDownload/"+fileName);
//  InputStream stream = new ByteArrayInputStream(content.getBytes("utf-8"));
    InputStream stream = IOUtils.toInputStream(content, "utf-8");
    Files.copy(stream, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
    System.out.println("File Downloaded")
}

but I am not able to read/open file eg MS excel file.但我无法读取/打开文件,例如 MS excel 文件。 While opening file got message as corrupted file.打开文件时收到消息为损坏的文件。

Just write byte array directly into file, without converting it to utf-8 string.只需将字节数组直接写入文件,无需将其转换为 utf-8 字符串。

import java.nio.file.Files;
import java.nio.file.Path;

Path file = Path.of("D:\\SharePoint\\TempDownload/"+fileName);
Files.write(file, response.getEntity().getContent()));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Apache TelnetClient,InputStream无法使用Apache IOUtils转换为String - Apache TelnetClient, InputStream unable to convert to String using Apache IOUtils 如何从String取回InputStream - How to get InputStream back from String 从findWithinHorizo​​n返回的Java大字符串转换为InputStream - Java large String returned from findWithinHorizon converted to InputStream java.lang.NoSuchMethodError:org.apache.poi.util.IOUtils.copy(Ljava / io / InputStream; Ljava / io / OutputStream;) - java.lang.NoSuchMethodError: org.apache.poi.util.IOUtils.copy(Ljava/io/InputStream;Ljava/io/OutputStream;) java.lang.NoSuchMethodError:org.apache.poi.util.IOUtils.copy(Ljava / io / InputStream; Ljava / io / OutputStream;)V - java.lang.NoSuchMethodError: org.apache.poi.util.IOUtils.copy(Ljava/io/InputStream;Ljava/io/OutputStream;)V 我应该关闭org.apache.commons.io.IOUtils的InputStream吗? - Should I close the InputStream of org.apache.commons.io.IOUtils 如果将Java Long转换为另一个基数,如何从转换后的String中取回它? - If you convert a Java Long to another base, how do you get it back from the String it was converted to? Java从IOUtils的InputStrem动态写入字符串数组,特别是copyBytes - Java writing dynamically to a String Array from InputStrem of a IOUtils, specifically copyBytes byte[] InputStream 转换为 String - byte[] InputStream converted to String java apache IOUtils中断文件内容 - java apache IOUtils breaks file content
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM