我一直在通过HttpURLConnection
关注上传文件的步骤outlined here。在我的例子中,有一个名为“json”的POST参数,需要退出服务器上的_POST['json']
。
问题是,_POST['json']
中没有任何内容,除非我尝试上传文件;文件数据放在_POST['json']
中,而不是_FILES['filename']
。
目标是实现这一目标:
_POST['json'] = {"fileData":"Some extra data about the file(s)"}
_FILES['filename'] = the file I uploaded.
我构建请求的代码是:
try {
url = new URL(_requestObject.getUrl());
conn = (HttpURLConnection)url.openConnection();
conn.setRequestProperty("Content-Type", MMWebAPIConstants.MM_REQUEST_CONTENT_TYPE_HF + MMWebAPIConstants.MM_REQUEST_BOUNDARY);
conn.addRequestProperty("Connection", "Keep-Alive");
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
if ( ! _requestObject.getAction().equals(MMWebAPIConstants.ACTION_LOGIN) ) {
for ( String cookie : MMSessionManager.getSharedInstance().getCookies() )
conn.addRequestProperty("Cookie", cookie);
}
outstream = conn.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outstream, "UTF-8"), true);
writer.append("--" + MMWebAPIConstants.MM_REQUEST_BOUNDARY).append(crlf);
writer.append("Content-Disposition: form-data; name=\"json\"").append(crlf);
writer.append("Content-Type: text/plain; charset=UTF-8").append(crlf);
writer.append(postValue.toString()).append(crlf).flush();
if ( hasfiles ) {
byte[] bytes = null;
InputStream input = null;
for ( String filename : _requestObject.getFilePaths() ) {
String filepath = this.getContext().getFilesDir().getAbsolutePath() + "/" + filename;
String type = URLConnection.guessContentTypeFromName(filename);
bytes = new byte[5120];
input = new FileInputStream(filepath);
writer.append("--" + MMWebAPIConstants.MM_REQUEST_BOUNDARY).append(crlf);
writer.append("Content-Disposition: form-data; name=\"" + filename + "\"; filename=\"" + filename + "\"").append(crlf);
writer.append("Content-Type: " + type).append(crlf);
writer.append("Content-Transfer-Encoding: binary").append(crlf);
writer.append(crlf).flush();
for ( int l = 0; (l = input.read(bytes)) > 0; ) {
outstream.write(bytes, 0, l);
}
outstream.flush();
writer.append(crlf).flush();
}
}
writer.append("--" + MMWebAPIConstants.MM_REQUEST_BOUNDARY + "--").append(crlf);
responseObject = this.getResponse(conn);
this.getCookiesFromRequest(conn);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if ( writer != null ) writer.close();
if ( conn != null ) conn.disconnect();
}
帖子和文件数组看起来像:
ERROR - 2012-08-02 18:49:59 --> POST: Array
(
[json] => PNG
IHDR#JzsBIT|d
4IDATx͒7rcd).`Tv-IHMH @55k.c_@qّ)f3Zz*UK%귿6}OGI~IJ~:M|@eӼXw|0`?b Fլq^$zAn.;Cضo=
9J2O; e:=Md?DI{a;Lr56xA+~ҍZO|{orB{Iu<VkWL<x<m=+ȡhE5$:ͲI\k `9_5Ofx7EX"D#'NO:ƧjS+uj:gjP=,rɳW3I s>DM4ޖ)մ_*``@JZ6(ʝZ3\ u5삮*rת@Rkc$o=JRs$%aϮi]ځl;]Q7lI6K \ `L?OAsشZQOٍߍ*Y7VyoZuJYq,iM`SyYUSJfjѮf["0%y5N_0Z5"\ߚ$/fC!-ca^Mpfizvߚ???,)ak"_bUE>ml,BcMSB[7y')O7I7c>m3;Or6]qrAfe8t]I>0Zc!6Ϡ8
YT0\YA\?<#'GXGI^c"<Ͳ]cU;k$'y/z$Ƣ&N g<-0Џr'IRJAŗ9|ʉj3IL[Yo<fugq2;-acY[cw/Ô#,U /X"0x,k>uF&LRN$snu;דO ~M</aSQ[Sy˶<*z`(RByP4Wh `L1=X s3ٖ'9VaJ WML
0&|k:MB|LRNĚU<S|z`AWt7010&)'v/z%{cn=6lU9dW]"۔Ϫ<LFH_cNoy.0&SNT"fR.{0* `l<RKr<Ͳ?O;ÔxyػI~lU;=Jr@] yMa&y=W}cbrƬs)aO_!<`삣$ea. jUC~ơ+=6Zu-!8HfJ.B `@MRNDW }\wlҽ$$ߏ=`@]%ɿ9 Uɓq͗g)!Ъ*b!0H `4q|=LVO7>#MW*yp. A͔m_/srr|We!]} 7GI˫OojRBK>gDK~@|܀W????[v9M8K9V4.Gtq܅'9+zؼU!U5z8:)Mr'9dy~ZB5IxE40 (Ƀ&NRNk(46+ܩU;}5ZnGI(EpA;I.n2Cۣ'`4pj5A]
ס
&ݱwb<gxGY .nqS*͞0СiسjV4uUߴvB2
!NV~r[;i`@5M n,k:٬خI4ZԾ6uIt1.Xs [Jg8ZW$tqQ)ByM3i/moJ@1mo7XnFi=&ͪWBU}Sp%i%xjMIENDB`
)
ERROR - 2012-08-02 18:49:59 --> FILES: Array
(
)
更新:
作为试用版,我尝试了this thread(它引发了运行时异常),我尝试了没有文件上传,但没有填充_POST['json']
,但仍然引发了运行时异常。
请寻求一些帮助。
答案 0 :(得分:-2)
看起来您没有正确添加帖子参数。你需要Post Parameters。
请参阅:Using HttpClient and HttpPost in Android with post parameters