我的上传代码如下:
String end = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try {
URL url = new URL(ActionUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestMethod("POST");
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Accept", "text/*");
con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
DataOutputStream ds = new DataOutputStream(con.getOutputStream());
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data;" + "name=\"folder\"" + end + end);
ds.write(SavePath.getBytes("UTF-8"));
ds.writeBytes(end);
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data;" + "name=\"Filedata\"; filename=\"");
ds.write(FileName.getBytes("UTF-8"));
ds.writeBytes("\"" + end);
ds.writeBytes(end);
FileInputStream fStream = new FileInputStream(uploadFilepath+""+FileName);
int bufferSize …Run Code Online (Sandbox Code Playgroud) 在Android代码中将多部分实体映像上传到服务器期间的状态代码500
Html格式:( 可以成功添加图像到服务器)
<form method="post" action="http://xyz/upload_picture" enctype="multipart/form-data">
Sample Picture Upload Form Submit
<br/><br/>
API key: <input type="text" name="key" value="abc"><br/><br/>
Login: <input type="text" name="login" value="text"><br/>
Password: <input type="password" name="password" value="text"><br/><br/>
Property ID:<input type="text" name="property_id" value="111"><br/>
Picture File:<input type="file" name="picture"><br/><br/>
<br/><br/>
<input type="submit" name="" value="Upload Picture"><br/>
</form>
Run Code Online (Sandbox Code Playgroud)
Android代码:( 状态代码500)
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://xyz/upload_picture");
try {
MultipartEntity entity = new MultipartEntity();
entity.addPart("key", new StringBody("abc"));
entity.addPart("login", new StringBody("abc"));
entity.addPart("password", new StringBody("test"));
entity.addPart("property_id", new StringBody("111"));
File file = …Run Code Online (Sandbox Code Playgroud)