MVC 中的 Web 服务,用于从 Android 获取多部分数据



我想在一个请求中将多个图像发送到Tomcat服务器。 为此,我需要在Spring MVC中编写Web服务,以在Java Spring MVC中获取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();
}

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

您可以尝试分离模型的方法,如下所示

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

您的 JSON 结果可以是我以这种方式完成的另一个 POJO 或字符串值

public class UploadResult {
String result;
String msg;

public UploadResult(String result, String msg) {
this.result = result;
this.msg = msg;
}
public String getResult() {
return result;
}

public String getMsg() {
return msg;
}

}

您的 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 = "/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();
}
}

相关内容

  • 没有找到相关文章