我在设置 HTTP Post android 应用程序代码以将文件、字符串或字节数组上传到 CherryPy 时遇到问题。有人可以指出我正确的方向吗?
我得到 HTTPError: (404, 'Missing parameters: myFile')。CherryPy 代码:
html += """ <h2>Upload a file</h2>
<form action="upload" method="post" enctype="multipart/form-data">
filename: <input type="file" name="myFile" /><br />
<input type="submit" />
</form></body></html>"""
def upload(self, myFile):
out = """<html>
<body>
myFile length: %s<br />
myFile filename: %s<br />
myFile mime-type: %s
</body>
</html>"""
size = 0
f = open(myFile.filename, 'w')
while True:
data = myFile.file.read(8192)
if not data:
break
size += len(data)
f.write(data)
return out % (size, myFile.filename, myFile.content_type)
upload.exposed = True
安卓应用代码:
String writeOut = "1,1234,4567,8910";
byte[] byteArray = writeOut.getBytes();
File tmpFile = new File("/sdcard/Download/tst.txt");
HttpParams params = new BasicHttpParams();
params.setParameter("myFile", "myfile1");
HttpConnectionParams.setConnectionTimeout(params, 15000);
HttpConnectionParams.setSoTimeout(params, 5 * 60 * 1000);
HttpClient client = new DefaultHttpClient(params);
HttpPost post = new HttpPost("http://192.168.1.123:8080/upload");
HttpEntity requestEntity = new FileEntity(tmpFile, "multipart/form-data;boundary=--");
post.setEntity(requestEntity);
HttpResponse response = (HttpResponse) client.execute(post);
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
String page = EntityUtils.toString(entity);
Log.d(HTTPTAG, "reply: " + page);
Log.d(HTTPTAG, "statuscode: " + statusCode);
记录 CherryPy 端:
192.168.1.114 - - [01/Aug/2012:10:10:25] "POST /upload HTTP/1.1" 404 1265 "" ""
Logcat端:
D/MyApp.HTTP( 6966): reply: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
D/MyApp.HTTP( 6966): "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
D/MyApp.HTTP( 6966): <html>
D/MyApp.HTTP( 6966): <head>
D/MyApp.HTTP( 6966): <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
D/MyApp.HTTP( 6966): <title>404 Not Found</title>
D/MyApp.HTTP( 6966): <style type="text/css">
D/MyApp.HTTP( 6966): #powered_by {
D/MyApp.HTTP( 6966): margin-top: 20px;
D/MyApp.HTTP( 6966): border-top: 2px solid black;
D/MyApp.HTTP( 6966): font-style: italic;
D/MyApp.HTTP( 6966): }
D/MyApp.HTTP( 6966):
D/MyApp.HTTP( 6966): #traceback {
D/MyApp.HTTP( 6966): color: red;
D/MyApp.HTTP( 6966): </style>
D/MyApp.HTTP( 6966): </head>
D/MyApp.HTTP( 6966): <body>
D/MyApp.HTTP( 6966): <h2>404 Not Found</h2>
D/MyApp.HTTP( 6966): <p>Missing parameters: myFile</p>
D/MyApp.HTTP( 6966): <pre id="traceback">Traceback (most recent call last):
D/MyApp.HTTP( 6966): File "/usr/local/lib/python2.7/dist-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/_cprequest.py", line 656, in respond
D/MyApp.HTTP( 6966): response.body = self.handler()
D/MyApp.HTTP( 6966): File "/usr/local/lib/python2.7/dist-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/lib/encoding.py", line 188, in __call__
D/MyApp.HTTP( 6966): self.body = self.oldhandler(*args, **kwargs)
D/MyApp.HTTP( 6966): File "/usr/local/lib/python2.7/dist-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/_cpdispatch.py", line 40, in __call__
D/MyApp.HTTP( 6966): raise sys.exc_info()[1]
D/MyApp.HTTP( 6966): HTTPError: (404, 'Missing parameters: myFile')
D/MyApp.HTTP( 6966): </pre>
D/MyApp.HTTP( 6966): <div id="powered_by">
D/MyApp.HTTP( 6966): <span>Powered by <a href="http://www.cherrypy.org">CherryPy 3.2.2</a></span>
D/MyApp.HTTP( 6966): </div>
D/MyApp.HTTP( 6966): </body>
D/MyApp.HTTP( 6966): </html>
D/MyApp.HTTP( 6966): statuscode: 404