我想从android上传图片到服务器。 我的Android ASYC代码:Spring with android image上传
final String jsonUserMo = gson.toJson(userMO);
final StringBuilder contactLists = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("userMO", jsonUserMo));
HttpPost post = new HttpPost(Constants.ROOTURL+"/media/uploadUserImage");
post.setEntity(new FileEntity(new File()));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
contactLists.append(rd.readLine());
} catch (Exception e) {
e.printStackTrace();
}
我的控制器代码:
@RequestMapping(value = { "/uploadUserImage" }, method = RequestMethod.POST)
public @ResponseBody
String uploadUserImage(@RequestParam(value = "uploadImg") MultipartFile file, @RequestParam("userMO") String userBO, HttpSession session, HttpServletRequest httpServletRequest) {
log.info("hitting image");
UserBO userBo = gson.fromJson(userBO, UserBO.class);
// jboss file location to store images
String filePath = httpServletRequest.getSession().getServletContext().getRealPath("/") + "\\resources\\userImages\\" + userBo.getRingeeUserId() + ".png";
String fileName = file.getOriginalFilename();
try {
if (!file.isEmpty() && file.getBytes().length >= 5242880) {
log.info("file size is "+file.getBytes());
}
if (!file.isEmpty()) {
//some logic
}
} catch (Exception Exp) {
log.info("Upload image failure");
}
return "";
}
我的servlet配置:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- <property name="maxUploadSize" value="5242880" /> -->
</bean>
我的问题是如何添加位图文件中httppost发送控制器。 链接:Unable to add MultipartEntity because its deprecated 否则工作从Android传递到控制器的Java对象。 我想从android [使用httppost]上传图像文件到控制器。 我的任何错误.. 请帮我吗?
post.setEntity(new FileEntity(new File(INSERT_PATH_TO_IMAGE_FILE_HERE)));? – Stan 2015-04-06 11:35:34
谢谢@ Stan.Now我尝试了 – nmkkannan 2015-04-06 11:37:32
我有一个疑问,任何可能在httppost中添加位图文件? – nmkkannan 2015-04-06 11:43:14