android - 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();
}
请帮助我使用网络服务。我面临很多麻烦
解决方案
下面的代码将读取 android/client 发送的图像。
@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();
}
}
推荐阅读
- powershell - Powershell reg 查询和表格形成
- php - PHP 与 MySQL 8.0+ 错误:服务器请求客户端未知的身份验证方法
- ngxs - 状态下嵌套对象的变化检测
- c# - Streamreader 读取同一目录中的文件
- c - C 使用 libconv 将 iso ISO-8859-1 转换为 UTF-8
- c++ - 应用程序内存使用量减少
- javascript - 如何将下一个上一个按钮添加到弹出窗口?
- mysql - 是否与空列联合
- html - 如何从 javafx webview 解析 html 并将这些数据传输到 Jsoup Document?
- laravel - 在 Laravel 5.7 中使电子邮件身份验证不区分大小写