Я хочу загрузить изображение с android на сервер. Мой андроид код asyc:Spring with android image uploading
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 "";
}
Мой сервлет конфигурации:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- <property name="maxUploadSize" value="5242880" /> -->
</bean>
Моя проблема заключается в том, чтобы добавить файл Bitmap в HttpPost отправить контроллер. Ссылка: Unable to add MultipartEntity because its deprecated В противном случае работает для передачи объекта java от android к контроллеру. Я хочу загрузить файл изображения с android [using httppost] на контроллер. Любые ошибки от меня .. , пожалуйста, помогите мне?
post.setEntity (новый FileEntity (новый файл (INSERT_PATH_TO_IMAGE_FILE_HERE)) ;? – Stan
Спасибо @ Stan.Now я попробую – nmkkannan
У меня есть одно сомнение, какие-либо возможности добавления файла растрового изображения в httppost? – nmkkannan