gpt4 book ai didi

java - 带有android图像上传的Spring

转载 作者:太空狗 更新时间:2023-10-29 14:10:20 25 4
gpt4 key购买 nike

我想从安卓上传图片到服务器。我的 android 异步代码:

    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();
}

我的 Controller 代码:

@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 中添加位图文件以发送 Controller 。链接:Unable to add MultipartEntity because its deprecated否则用于将 java 对象从 android 传递到 Controller 。我想从 android [使用 httppost] 上传图像文件到 Controller 。我的任何错误..请帮帮我?

最佳答案

        final File file1 = new File(url_path);

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(http_url_path1);
FileBody bin1 = new FileBody(file1);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("abc", new StringBody(abcid));
reqEntity.addPart("xyz", new StringBody(xyzid));
reqEntity.addPart("file", bin1);
reqEntity.addPart("key", new StringBody(Key));
reqEntity.addPart("authentication_token", new StringBody(Authe_Key));
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
resEntity = response.getEntity();

希望这会奏效...

关于java - 带有android图像上传的Spring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29470370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com