这是我的代码
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.hugosys.in/www.nett-torg.no/api/rpcs/uploadfiles/?");
File file = new File("/mnt/sdcard/xperia.png");
FileBody fileBody = new FileBody(file);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("post_id", new StringBody("906"));
reqEntity.addPart("user_id", new StringBody("1"));
reqEntity.addPart("files", fileBody);
httpPost.setEntity(reqEntity);
response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
response_string =convertStreamToString(is);
..........
解析响应的方法
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append((line + "n"));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
我从服务器上获得了成功,但服务器上没有收到图像。。。为什么我做了错误的
应用此
private字符串doFileUpload(){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
myimage.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] imagebyte = stream.toByteArray();
System.out.println(imagebyte + "...................................");
String urlString = "your url";
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(urlString);
MultipartEntity reqEntity = new MultipartEntity();
Calendar cal = Calendar.getInstance();
String image_name = cal.getTime().toString();
image_name = image_name.replace(" ", "");
image_name = image_name.replace(":", "");
image_name = image_name.substring(3, 14);
//userfile
reqEntity.addPart("featured_img", new ByteArrayBody(imagebyte,
"Chasin_" + image_name + "_image.jpg"));
reqEntity.addPart("title", new StringBody(title_var));
reqEntity.addPart("description",new StringBody(description_var));
reqEntity.addPart("category",new StringBody(cat_var));
reqEntity.addPart("tags",new StringBody(tag));
reqEntity.addPart("userid",new StringBody(Constants.USER_ID));
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
HttpEntity resEntity = response.getEntity();
response_str = EntityUtils.toString(resEntity);
} catch (Exception ex) {
Log.e("Debug", "error: " + ex.getMessage(), ex);
}
return response_str;
}
伙计们,我想我必须做这些事情
public void connectForMultipart() throws Exception {
con = (HttpURLConnection) ( new URL(url)).openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
con.connect();
os = con.getOutputStream();
}
public void addFormPart(String paramName, String value) throws Exception {
writeParamData(paramName, value);
}
public void addFilePart(String paramName, String fileName, byte[] data) throws Exception {
os.write( (delimiter + boundary + "rn").getBytes());
os.write( ("Content-Disposition: form-data; name="" + paramName + ""; filename="" + fileName + ""rn" ).getBytes());
os.write( ("Content-Type: application/octet-streamrn" ).getBytes());
os.write( ("Content-Transfer-Encoding: binaryrn" ).getBytes());
os.write("rn".getBytes());
os.write(data);
os.write("rn".getBytes());
}
public void finishMultipart() throws Exception {
os.write( (delimiter + boundary + delimiter + "rn").getBytes());
}
我已经将Content-Type: application/octet-streamrn"
更改为Content-Type: image/jpegrnrn
,它起作用了:)
File imgFile = new File(Environment.getExternalStorageDirectory() + "/photo1/image2.jpg");
if (imgFile.exists()) {
myimage = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
// im.setImageBitmap(myimage);
}