Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.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
从java端上传文件到REST是可行的,而不是从python_Java_Python_Rest_File Upload - Fatal编程技术网

从java端上传文件到REST是可行的,而不是从python

从java端上传文件到REST是可行的,而不是从python,java,python,rest,file-upload,Java,Python,Rest,File Upload,我们正在测试Saperion REST web服务,除文件上传外,一切正常。java方面一切正常: public HttpResponse insertDocument(String docId) throws IOException { RestClient client = new RestClient(); byte[][] file1 = null; System.out.println("insertDocument: Reading attachment

我们正在测试Saperion REST web服务,除文件上传外,一切正常。java方面一切正常:

public HttpResponse insertDocument(String docId) throws IOException {

    RestClient client = new RestClient();

    byte[][] file1 = null;
    System.out.println("insertDocument: Reading attachment files to insert...");
    try {

        file1 = new byte[][]
                {

                 client.readFile("/usr/local/www/test.pdf")

                };

    } catch (Exception ex) {
        System.out.println("******** insertDocument catch ******");
        System.out.println("insertDocument: File not read...");
        System.out.println("insertDocument: Error: " + ex.getMessage());
    }

    HttpClient httpclient = new DefaultHttpClient();

    try {
        String url = SERVER + INSERT_DOCUMENT_OPERATION;
        System.out.println("insertDocument: url - " + url + "/" + docId);

        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Accept", "text/plain");
        httpPost.setHeaders(getAuthentication());


        // META
        String str = "<query id=\"001\">" +
                "<docid>" + docId + "</docid>" +
                "<dochead>" +
                "</dochead>" +
                "<attachments count=\"" + file1.length + "\">";


        for (int i = 0; i < file1.length; i++) {
            str += "<entry><filename>" + docId + "_" + i + ".pdf" + "</filename>" +
                    "<filedata/>" +
                    "</entry>";
        }

        str += "</attachments>" +
                "</query>";

        System.out.println("insertDocument: str - " + str);

        MultipartEntity entity = new MultipartEntity();
        entity.addPart("xmlString", new StringBody(str));

        for (byte[] file : file1)
            entity.addPart("file", new ByteArrayBody(file, "file"));

        httpPost.setEntity(entity);


        HttpResponse response = httpclient.execute(httpPost);

        System.out.println("insertDocument: return response ...");
        return response;

    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}
public HttpResponse insertDocument(字符串docId)引发IOException{
RestClient=newrestclient();
字节[][]file1=null;
System.out.println(“插入文档:读取要插入的附件文件…”);
试一试{
file1=新字节[][]
{
client.readFile(“/usr/local/www/test.pdf”)
};
}捕获(例外情况除外){
System.out.println(“*********insertDocument catch*******”);
System.out.println(“insertDocument:File not read…”);
System.out.println(“insertDocument:Error:+ex.getMessage());
}
HttpClient HttpClient=新的DefaultHttpClient();
试一试{
字符串url=服务器+插入文档\u操作;
System.out.println(“insertDocument:url-”+url+“/”+docId);
HttpPost HttpPost=新的HttpPost(url);
httpPost.setHeader(“接受”、“文本/普通”);
setHeaders(getAuthentication());
//元
字符串str=“”+
“”+docId+“”+
"" +
"" +
"";
for(int i=0;i
这是请求内容的外观:

POST /saperion/services/rest/insertDocument HTTP/1.1
RC_USER: user
RC_USER_PASSWORD: pass
Content-Length: 8476
Content-Type: multipart/form-data; boundary=3mzL6oR0- E0dudMZ_pV3OVTE41pV4yH
Host: appdevel4.kada.lan
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.2.2 (java 1.5)
--3mzL6oR0-E0dudMZ_pV3OVTE41pV4yH

Content-Disposition": "form-data; name="xmlString"
Content-Type: text/plain; charset=US-ASCII"
Content-Transfer-Encoding: 8bit
<query id="001"><docid>55</docid><dochead></dochead><attachments     count="1"><entry><filename>55_0.pdf</filename><filedata/></entry></attachments></query>
--3mzL6oR0-E0dudMZ_pV3OVTE41pV4yH
Content-Disposition:form-data; name="file"; filename="file"
Content-Type: application/octet-stream
Content-Transfer-Encoding: "binary"
binary pdf content
--3mzL6oR0-E0dudMZ_pV3OVTE41pV4yH
POST/saperion/services/rest/insertDocument HTTP/1.1
用户:用户
RC_用户_密码:pass
内容长度:8476
内容类型:多部分/表单数据;边界=3mzL6oR0-E0dudMZ_pV3OVTE41pV4yH
主机:appdevel4.kada.lan
连接:保持活力
用户代理:Apache HttpClient/4.2.2(java 1.5)
--3mzL6oR0-E0dudMZ_pV3OVTE41pV4yH
内容配置“:”表单数据;name=“xmlString”
内容类型:文本/纯文本;字符集=US-ASCII“
内容传输编码:8比特
5555_0.pdf
--3mzL6oR0-E0dudMZ_pV3OVTE41pV4yH
内容配置:表单数据;name=“file”;filename=“file”
内容类型:应用程序/八位字节流
内容传输编码:“二进制”
二进制pdf内容
--3mzL6oR0-E0dudMZ_pV3OVTE41pV4yH
下面是python端的代码:

xml="""<query id="001">
        <docid>56</docid>
        <dochead></dochead>
        <attachments count="1">
            <entry>
                <filename>56_0.pdf</filename>
                <filedata/>
            </entry>
        </attachments>
        </query>"""


url="http://appdevel4.kada.lan/saperion/services/rest/insertDocument/56"
files = {'xmlString': (None, xml, "text/plain; charset=US-ASCII", {'Content-Transfer-Encoding':'8bit'} ), 'file': ('file', open('/usr/local/www/test.pdf', 'rb'), "application/octet-stream", {'Content-Transfer-Encoding':'binary'})}
    headers={
    'Host':'appdevel4.kada.lan',
    'POST': '/saperion/services/rest/insertDocument HTTP/1.1',
    'Connection': 'Keep-Alive',
    'RC_USER':'user', 
    'RC_USER_PASSWORD':'passwd'
    }
    response = requests.post("http://appdevel4.kada.lan/saperion/services/rest/insertDocument/55", files=files, headers=headers)
xml=”“”
56
56_0.pdf
"""
url=”http://appdevel4.kada.lan/saperion/services/rest/insertDocument/56"
files={'xmlString':(无,xml,“text/plain;charset=US-ASCII”,{'Content-Transfer-Encoding':'8bit'}),'file':('file',open('/usr/local/www/test.pdf','rb'),'application/octet stream',{'Content-Transfer-Encoding':'binary'})
标题={
“主机”:“appdevel4.kada.lan”,
“POST”:“/saperion/services/rest/insertDocument HTTP/1.1”,
“连接”:“保持活动状态”,
“RC_用户”:“用户”,
“RC_用户_密码”:“passwd”
}
响应=请求。发布(“http://appdevel4.kada.lan/saperion/services/rest/insertDocument/55,文件=文件,标题=标题)
以下是python请求的内容:

POST http://appdevel4.kada.lan/saperion/services/rest/insertDocument/56
Content-Length: 8521
Connection: Keep-Alive
Host: appdevel4.kada.lan
Cookie: JSESSIONID=li0vmwnqKmtcWF2sWk06cFWG
RC_USER: user
RC_USER_PASSWORD: passwd
POST: /saperion/services/rest/insertDocument HTTP/1.1
Content-Type: multipart/form-data; boundary=477579b8a0b04c9286c7c78ce3bb72b5

--477579b8a0b04c9286c7c78ce3bb72b5
Content-Disposition: form-data; name="xmlString"
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 8bit

<query id="001">
<docid>56</docid>
<dochead></dochead>
<attachments count="1">
<entry>
<filename>56_0.pdf</filename>
<filedata/>
</entry>
</attachments>
</query>

--477579b8a0b04c9286c7c78ce3bb72b5
Content-Disposition: form-data; name="file"; filename="file"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
PDF content
--477579b8a0b04c9286c7c78ce3bb72b5--
POSThttp://appdevel4.kada.lan/saperion/services/rest/insertDocument/56
内容长度:8521
连接:保持活力
主机:appdevel4.kada.lan
Cookie:JSESSIONID=li0vmwnqKmtcWF2sWk06cFWG
用户:用户
RC_用户_密码:passwd
POST:/saperion/services/rest/insertDocument HTTP/1.1
内容类型:多部分/表单数据;边界=477579b8a0b04c9286c7c78ce3bb72b5
--477579b8a0b04c9286c7c78ce3bb72b5
内容处置:表单数据;name=“xmlString”
内容类型:文本/普通;字符集=US-ASCII
内容传输编码:8比特
56
56_0.pdf
--477579b8a0b04c9286c7c78ce3bb72b5
内容配置:表单数据;name=“file”;filename=“file”
内容类型:应用程序/八位字节流
内容传输编码:二进制
PDF内容
--477579b8a0b04c9286c7c78ce3bb72b5--

因此,java和python请求的内容实际上是相同的,但java上传成功,但python生成的404未找到。

您的帖子URL在java和python中是不同的。在java中是
/saperion/services/rest/insertDocument
,而在python中是
http://appdevel4.kada.lan/saperion/services/rest/insertDocument/56
。在进行比较之前,请确保您的请求是相同的。

您的帖子URL在Java和Python中是不同的。在Java中是
/saperion/services/rest/insertDocument
,而在Python中是
http://appdevel4.kada.lan/saperion/services/rest/insertDocument/56
。确保您的请求是相同的,然后才能进行比较回复。

谢谢:)在Python中只保留insertDocument修复了该问题。谢谢:)在Python中只保留insertDocument修复了该问题。