Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mvc中的web服务从android获取多部分数据_Android_Web Services_Spring Mvc - Fatal编程技术网

mvc中的web服务从android获取多部分数据

mvc中的web服务从android获取多部分数据,android,web-services,spring-mvc,Android,Web Services,Spring Mvc,我想在一个请求中向tomcat服务器发送多个图像。为此,我需要在SpringMVC中编写web服务,以在JavaSpringMVC中获取android的多部分实体 下面是我的android代码 public void upload() throws Exception { //Url of the server String url ="http://10.21.xxx.xxx:1010/MultiFileUpload/test"; HttpClient clie

我想在一个请求中向tomcat服务器发送多个图像。为此,我需要在SpringMVC中编写web服务,以在JavaSpringMVC中获取android的多部分实体

下面是我的android代码

    public void upload() throws Exception {
    //Url of the server
    String url ="http://10.21.xxx.xxx:1010/MultiFileUpload/test";
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);
    MultipartEntity mpEntity = new MultipartEntity();
    //Path of the file to be uploaded
    String filepath = "";

//Add the data to the multipart entity
    File file1 = new File(filepath);
    ContentBody cbFile1 = new FileBody(file1, "image/jpeg");
mpEntity.addPart("image", cbFile);

File file2 = new File(filepath);
    ContentBody cbFile2 = new FileBody(file2, "image/jpeg");
mpEntity.addPart("image", cbFile);

File file3 = new File(filepath);
    ContentBody cbFile3 = new FileBody(file3, "image/jpeg");
mpEntity.addPart("image", cbFile);

    mpEntity.addPart("name", new StringBody("Test", Charset.forName("UTF-8")));
    mpEntity.addPart("data", new StringBody("This is test report", Charset.forName("UTF-8")));


      post.setEntity(mpEntity);
    //Execute the post request
    HttpResponse response1 = client.execute(post);
    //Get the response from the server
    HttpEntity resEntity = response1.getEntity();
    String Response= EntityUtils.toString(resEntity);
    Log.d("Response:", Response);
    //Generate the array from the response
    JSONArray jsonarray = new JSONArray("["+Response+"]");
    JSONObject jsonobject = jsonarray.getJSONObject(0);
    //Get the result variables from response
    String result = (jsonobject.getString("result"));
    String msg = (jsonobject.getString("msg"));
    //Close the connection
    client.getConnectionManager().shutdown();
}

请帮助我使用web服务。我面临很多麻烦

下面的代码将读取安卓/客户端发送的图像

@RequestMapping(value = "/test", method = RequestMethod.POST)
    public ResponseEntity < String > test(HttpServletRequest request,HttpServletResponse response) {

     byte[] imageBytes = null;

     try {
      MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
      for (Entry < String, MultipartFile > entry: multipartRequest.getFileMap().entrySet()) {
       imageBytes = entry.getValue().getBytes();
      }
     } catch (Exception e) {
      e.printStackTrace();
     }
    }
@RequestMapping(value=“/test”,method=RequestMethod.POST)
公共响应属性测试(HttpServletRequest请求,HttpServletResponse响应){
byte[]imageBytes=null;
试一试{
MultipartTTpServletRequest multipartRequest=(MultipartTTpServletRequest)请求;
对于(条目Entry:multipartRequest.getFileMap().entrySet()){
imageBytes=entry.getValue().getBytes();
}
}捕获(例外e){
e、 printStackTrace();
}
}

您可以尝试以下分离模型的方法

public class MultiImage implements Serializable
{
    private static final long serialVersionUID = 74458L;

    private String name;
    private String data;

    private List<MultipartFile> images;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getData() {
        return data;
    }
    public void setData(String data) {
        this.data = data;
    }
    public List<MultipartFile> getImages() {
        return images;
    }
    public void setImages(List<MultipartFile> images) {
        this.images = images;
    }
}
您的MVC/服务端将处理此请求,方法是遍历模型并获取图像,然后将其传输或保存到预定义位置,并将结果包装到ResponseEntity中

@RequestMapping(value = "/upload-image", method = RequestMethod.POST)
    public ResponseEntity uploadImages(HttpServletRequest servletRequest, @ModelAttribute MultiImage multiImage, Model model) {
        //Get the uploaded images and store them
        List<MultipartFile> images = multiImage.getImages();
        List<String> fileNames = new ArrayList<String>();
        if (null != images && images.size() > 0)
        {
            for (MultipartFile multipartFile : images) {
                String fileName = multipartFile.getOriginalFilename();
                fileNames.add(fileName);
                File imageFile = new File(servletRequest.getServletContext().getRealPath("/image"), fileName);
                try
                {
                    multipartFile.transferTo(imageFile);
                } catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        model.addAttribute("images", images);
        UploadResult uploadResult = new UploadResult("Success","Images Uploaded");
        return ResponseEntity.Ok(uploadResult);
    }
@RequestMapping(value=“/upload image”,method=RequestMethod.POST)
公共响应上载映像(HttpServletRequest servletRequest,@ModelAttribute MultiImage MultiImage,Model Model){
//获取上传的图像并存储它们
List images=multiImage.getImages();
列表文件名=新的ArrayList();
if(null!=images&&images.size()>0)
{
用于(多部分文件多部分文件:图像){
字符串文件名=multipartFile.getOriginalFilename();
添加(文件名);
File imageFile=新文件(servletRequest.getServletContext().getRealPath(“/image”),文件名);
尝试
{
multipartFile.transferTo(imageFile);
}捕获(IOE异常)
{
e、 printStackTrace();
}
}
}
model.addAttribute(“图像”,图像);
UploadResult UploadResult=新的UploadResult(“成功”,“上传图像”);
返回ResponseEntity.Ok(上传结果);
}

谢谢@rizwan u让我开心
@RequestMapping(value = "/upload-image", method = RequestMethod.POST)
    public ResponseEntity uploadImages(HttpServletRequest servletRequest, @ModelAttribute MultiImage multiImage, Model model) {
        //Get the uploaded images and store them
        List<MultipartFile> images = multiImage.getImages();
        List<String> fileNames = new ArrayList<String>();
        if (null != images && images.size() > 0)
        {
            for (MultipartFile multipartFile : images) {
                String fileName = multipartFile.getOriginalFilename();
                fileNames.add(fileName);
                File imageFile = new File(servletRequest.getServletContext().getRealPath("/image"), fileName);
                try
                {
                    multipartFile.transferTo(imageFile);
                } catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        model.addAttribute("images", images);
        UploadResult uploadResult = new UploadResult("Success","Images Uploaded");
        return ResponseEntity.Ok(uploadResult);
    }