尝试使用python和请求将文件发布到php服务器

尝试使用python和请求将文件发布到php服务器,php,python,python-requests,Php,Python,Python Requests,我正在尝试模拟这个java代码 public static void uploadFile(String filename, String systemID){ try{ String createNew = "false"; //check for backup files to know if we should make a new file on the se

我正在尝试模拟这个java代码

            public static void uploadFile(String filename, String systemID){
            try{
                    String createNew = "false";

                    //check for backup files to know if we should make a new file on the server
                    File f = new File(filename + ".1");
                    if(f.exists()){
                            createNew = "true";
                            f.delete();
                    }

                    HttpURLConnection httpUrlConnection = (HttpURLConnection)new URL(uploadURL).openConnection();
            httpUrlConnection.setDoOutput(true);
            httpUrlConnection.setRequestMethod("POST");
            httpUrlConnection.setRequestProperty("Content-encoding", "deflate");
            httpUrlConnection.setRequestProperty("Content-type", "application/octet-stream");
            java.io.OutputStream os = httpUrlConnection.getOutputStream();
            Thread.sleep(1000);

            String fileData = IOUtils.toString(new FileReader(filename));

            String request = "filedata=" + fileData + "&filename=" + filename + "&systemid=" + systemID + "&createNew=" + createNew;

            DeflaterOutputStream deflate = new DeflaterOutputStream(os);
            deflate.write(request.getBytes());
            deflate.flush();
            deflate.close();

            os.close();
            BufferedReader in = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream()));

            String s = null;
            while ((s = in.readLine()) != null) {
                System.out.println(s);
            }
            in.close();
            //fis.close();
            }catch(Exception e){
                    e.printStackTrace();
            }
            static String uploadURL = "http://vp-creations.com/utilities/fileupload.php";
在使用请求的python中,我想我遗漏了一些简单的东西,因为我无法从服务器获得正确的响应

import requests
url = 'http://vp-creations.com/utilities/fileupload.php'
files = {'file': open('C:\\etc\\guitartab.txt', 'rb')}
headers = {'Content-encoding': 'deflate', 'Content-type': 'application/octet-stream'}

payload = {'filedata=': 'foo', 'filename=': 'bar', 'systemid=' : 'fooe', 'createNew=' : 'false'}

r = requests.post(url, files=files, headers=headers, data=payload)
我一直从服务器得到的响应是 {“response”:“error”,“comment”:“缺少至少一个参数”}”
有什么帮助吗?

当您将字典传递到
请求的
数据
参数。post
时,它将被表单编码。我并不完全熟悉Java代码,但它看起来实际上使用了一个编码的
字符串作为请求的数据。为了实现与
请求
相同的功能,请传递一个字符串作为
数据
参数,如下所示:

data = 'filedata=foo&filename=bar&systemid=&fooe&createNew=false'
r = requests.post(url, files=files, headers=headers, data=data)

请参阅文档中的类似示例。

您需要尝试更改以下内容

首先,
data
在POST正文中发送给定的字典。如果这是您真正想要的,则需要从字典中删除“=”元素:

payload = {'filedata': 'foo', 'filename': 'bar', 'systemid' : 'fooe', 'createNew' : 'false'}
其次,您添加了一对不正确的头。当您使用Requests'
file
参数时,Requests将主体数据(包括您的文件)准备为
multipart/form data'
。然后用
应用程序/octet流
覆盖该内容类型,这不是正文的形式。此外,您声称您的身体数据是使用
deflate
压缩的,但事实并非如此

请尝试以下代码:

import requests
url = 'http://vp-creations.com/utilities/fileupload.php'
files = {'file': open('C:\\etc\\guitartab.txt', 'rb')}

payload = {'filedata': 'foo', 'filename': 'bar', 'systemid' : 'fooe', 'createNew' : 'false'}

r = requests.post(url, files=files, data=payload)

如果您执行
'createNew=':'true'
,会发生什么情况?另外,您是否验证了java代码是否正常工作?是的,java代码正常工作了,我已经使用它好几个星期了,我尝试了true,但仍然无效。这确实不起作用,但我将有效负载更改为
有效负载={'filedata':'foo','systemid':'fooe','createNew':'false','filename':'bar'}
然后我使用了它,但它仍然不起作用
r=requests.post(url,files=files,headers=headers,params=payload)
尝试了一下,仍然不起作用。得到了与以前相同的错误。我想我需要那些包含在java文件中的标题b/c