2015-04-06 71 views
3

我想从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]上传图像文件到控制器。 我的任何错误.. 请帮我吗?

+0

post.setEntity(new FileEntity(new File(INSERT_PATH_TO_IMAGE_FILE_HERE)));? – Stan 2015-04-06 11:35:34

+0

谢谢@ Stan.Now我尝试了 – nmkkannan 2015-04-06 11:37:32

+0

我有一个疑问,任何可能在httppost中添加位图文件? – nmkkannan 2015-04-06 11:43:14

回答

0

毕竟当你连续两次setEntity不是第二个在这里写入/取消第一组:?

post.setEntity(new FileEntity(new File())); 
post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

而关于传递一个文件:当你通过的​​内new FileEntity()就像我在评论说,你应该添加到文件的路径:

post.setEntity(new FileEntity(new File("path_to_a_file"))); 

如果你想通过一个位图从ImageView中有一些选项。您caould第一存储位图到一个PNG或JPEG文件,然后传递文件:

 final File imageFile = File.createTempFile();// temp file to store Bitmap to 
     // Convert bitmap to byte array 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
// here you have a stream - you could try yo upload it if you want to 
     // compressing Bitmap to a PNG 
     bitmap.compress(CompressFormat.PNG, 100, bos); 

     // write the bytes in file 
     isSucceed = FileUtils.byteArrayOutputStreamToFile(bos, imageFile); 
     // if (isSucceed) your Bitmap is stored into a file successfully 
     // closing a stream 
     if (bos != null) 
      try { 
       bos.close(); 
      } catch (IOException e) {} 

或者可以尝试到您的位图上传作为流。
你也应该在MIME类型添加到FileEntity构造(like here):

new FileEntity(new File(),"image/jpeg;"); 

也作出适当多上传这些文章可能会有所帮助:
Upload files by sending multipart request programmatically

A good approach to do multipart file upload in Android

+0

Thanks @ Stan,在httppost中添加位图文件的任何可能性? – nmkkannan 2015-04-06 11:44:20

+0

'new File(“/ mnt/photos/my_bitmap.bmp”)'如果它真的是位图文件 - http://en.wikipedia.org/wiki/BMP_file_format或'new File(“/ mnt/photos/my_bitmap。 jpg“)'因为你提到了一个文件。 – Stan 2015-04-06 11:46:43

+0

@感谢您的帮助..我会检查它.. – nmkkannan 2015-04-06 11:47:26

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

希望这会工作...

+0

MultipartEntity已弃用,或者我没有获取MultipartEntity的资源 – nmkkannan 2015-04-08 06:11:11