Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从web服务器url读取.docx内容-java_Java_File_Http_Inputstream_Docx - Fatal编程技术网

从web服务器url读取.docx内容-java

从web服务器url读取.docx内容-java,java,file,http,inputstream,docx,Java,File,Http,Inputstream,Docx,我有存储文档的WEBDAV服务器。它们可以通过url e.q获得。现在我想拿些文件读一下他的内容。我所拥有的: public void getDocumentContent() throws ExternalIntegrationException { var client = getHttpClient(); var download = new HttpGet(doc); try {

我有存储文档的WEBDAV服务器。它们可以通过url e.q获得。现在我想拿些文件读一下他的内容。我所拥有的:

public void getDocumentContent() throws ExternalIntegrationException {
        var client = getHttpClient();
       
            var download = new HttpGet(doc);
            try {
                InputStream input = client.execute(download).getEntity().getContent();
                String str = IOUtils.toString(input, StandardCharsets.UTF_8);
                System.out.println(str);
            } catch(IOException e) {
                throw new ExternalIntegrationException("Failure download file from " + webDavPath  + ". " +
                        "Details:" + e.getMessage(), e);
            }
        
    }

private HttpClient getHttpClient() {
        var credentialsProvider = new BasicCredentialsProvider();
        var credentials = new UsernamePasswordCredentials(userName, password);
        credentialsProvider.setCredentials(AuthScope.ANY, credentials);

        return HttpClientBuilder.create()
                .setDefaultCredentialsProvider(credentialsProvider)
                .build();
    }
my System.out.printl(用于测试)在控制台中获取:

X�K����nDUA*�)Y����ă�ښl  1i�J�/z,'��nV���K~ϲ��)a���m ����j0�Hu�T�9bx�<�9X�
�Q���
�Iʊ~���8��W�Z�"V0}����������>����uQwHo��   �� PK    ! ���   N  _rels/.rels �(�                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 ���JA���a�}7�
ig�@��X6_�]7~
f��ˉ�ao�.b*lI�r�j)�,l0�%��b�
6�i���D�_���,   �   ���|u�Z^t٢yǯ;!Y,}{�C��/h>  �� PK    ! �d�Q�   1  word/_rels/document.xml.rels �(�                                                                                                                                                                                                                                                                 ���j�0���{-;���ȹ�@��� �����$���~�
�U�>�0̀�"S�+a_݃(���vuݕ���c���T�/<�!s��Xd3�� �����?'g![�?��4���%�9���R�k6��$C�,�`&g�!/=�  �� PK    ! �^��  "     word/document.xml�W]o�0}����y� ��"B���=T+�&�k�����wV���*�D�����s�mfW?
��k���0"�T3�6  yX��$p�*F�V��=8r5�n���Ns  ��\\���{��K� �j
��[��S���|��,�)Ԧ�m�<5�*bhA �ܖנ�ע��mR�$���ٷ3m�1KwX)�w�2cu
�/����k�ga���Իۺ�⪽cgh���� 2_-�WA���`ô�x=�L�7��6�J�� ^ɶ�u:O'�cJ���2O�f:[Z���`�!�=��L,�!w��/�;��-���ٰK���<j�,��r>������/V<�B�~T�q�A����:������ZU��O7ܥx������Ͽ^h�b�^h��`���N�d�U�:��������s�r�Y��1��~��]㓿UϽ��]<��woO �F�ڟ
R�T����ߊ�9��q�Z
X�K����nDUA*�)Y����ă�ښl 1i�J�/z、 "��内华达州���K~ϲ��)A.���M����j0�胡�T�9bx� 为什么它对您不起作用?

由于docx是一种基于xml的纯文本格式,其中包含二进制blob,因此不能简单地将文档打印为字符串

解决方案:

我建议将文件保存在本地,并将其作为FileInputStream打开。 只需删除结尾处的文件

如果无法在本地保存文件,则可以

将变量“input”设置为FileInputStream后,可以使用以下代码:

import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
    public void readDocxFile(FileInputStream input) {
            try {

                XWPFDocument document = new XWPFDocument(input);

                List<XWPFParagraph> paragraphs = document.getParagraphs();


                for (XWPFParagraph para : paragraphs) {
                    System.out.println(para.getText());
                }
                input.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
导入java.io.File;
导入java.io.FileInputStream;
导入java.util.List;
导入org.apache.poi.xwpf.usermodel.XWPFDocument;
导入org.apache.poi.xwpf.usermodel.XWPFParagraph;
public void readDocxFile(FileInputStream输入){
试一试{
XWPF文档=新的XWPF文档(输入);
列表段落=document.getPages();
对于(XWPF段落:段落){
System.out.println(para.getText());
}
input.close();
}捕获(例外e){
e、 printStackTrace();
}
}
为什么它对您不起作用?

由于docx是一种基于xml的纯文本格式,其中包含二进制blob,因此不能简单地将文档打印为字符串

解决方案:

我建议将文件保存在本地,并将其作为FileInputStream打开。 只需删除结尾处的文件

如果无法在本地保存文件,则可以

将变量“input”设置为FileInputStream后,可以使用以下代码:

import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
    public void readDocxFile(FileInputStream input) {
            try {

                XWPFDocument document = new XWPFDocument(input);

                List<XWPFParagraph> paragraphs = document.getParagraphs();


                for (XWPFParagraph para : paragraphs) {
                    System.out.println(para.getText());
                }
                input.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
导入java.io.File;
导入java.io.FileInputStream;
导入java.util.List;
导入org.apache.poi.xwpf.usermodel.XWPFDocument;
导入org.apache.poi.xwpf.usermodel.XWPFParagraph;
public void readDocxFile(FileInputStream输入){
试一试{
XWPF文档=新的XWPF文档(输入);
列表段落=document.getPages();
对于(XWPF段落:段落){
System.out.println(para.getText());
}
input.close();
}捕获(例外e){
e、 printStackTrace();
}
}