使用retrofit上传多张图片和文字,java后台
2017-09-30 16:40
841 查看
废话不多说直接上代码:
Android端代码:
具体上传图片的代码:
接口的地址:
后台使用java的ssm框架搞的,代码如下:
/**
* 上传文件
*
* @param session
* @param file
* @return
*
*/
@RequestMapping(value = "/house/uploadPhoto", produces"text/html;charset=UTF-8")
@ResponseBody
public String fileUpload(HttpSession session,
@RequestParam(value = "file", required = true) MultipartFile[] multiFiles,
String houseCode) {
for (MultipartFile multipartFile : multiFiles) {
String path = Property.getProperty("fileUpload.path");
File dir = new File(path);
if (!dir.exists()) {
dir.mkdir();
}
String filename = "";
if (!multipartFile .isEmpty()) {
// log.debug(session.getServletContext().getRealPath(path));
try {
// 用UUID而不是时间戳
filename = UUID.randomUUID().toString() + "____" +
multipartFile .getOriginalFilename();
// 文件保存路径
String filePath = path + "/" + filename;
// 转存文件
multipartFile .transferTo(new File(filePath));
} catch (Exception e) {
log.error(CustomStringUtils.getExceptionInfo(e));
return JSONUtil.toJsonString(new JsonResult(-1, "上传发生错误!", null));
}
}
}
return JSONUtil.toJsonString(new JsonResult(1, "上传成功!", null));
}
记得在springMvc.xml文件中配置上传的设置:
<!-- 上传文件bean -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
<property name="maxUploadSize"
value="10485760000" /> <property name="maxInMemorySize" value="40960" />
</bean>
切记Android中的
要和RequestParam中的value的值一样。
Android端代码:
case R.id.save_housephoto://上传图片 new Thread(){ @Override public void run() { super.run(); upLoadPhoto(); } }.start(); break;
具体上传图片的代码:
//上传图片 private void upLoadPhoto() { if(imagePaths.size()==0){ Toast.makeText(AddpointActivity.this,"请选择图片",Toast.LENGTH_SHORT).show(); return; } Map<String, RequestBody> photos = new HashMap<>(); if (imagePaths.size()>0) { for (int i=0;i<imagePaths.size();i++) { String substring = imagePaths.get(i).substring(imagePaths.get(i).lastIndexOf("/") + 1, imagePaths.get(i).length()); photos.put("file\"; filename=\""+substring,RequestBody.create(MediaType.parse("multipart/form-data"), new File(imagePaths.get(i)))); } } Retrofit retrofit=new Retrofit.Builder() .baseUrl(Constant.BASEURL) .addConverterFactory(GsonConverterFactory.create()) .build(); PhotoUploadApi photoapi= retrofit.create(PhotoUploadApi.class); Call<JsonResult> call = photoapi.uploadPhoto(housecode, photos); call.enqueue(new Callback<JsonResult>() { @Override public void onResponse(Call<JsonResult> call, Response<JsonResult> response) { Log.i(TAG, "onResponse:成功 "+response.body().toString()); } @Override public void onFailure(Call<JsonResult> call, Throwable t) { } }); }
接口的地址:
//房屋多图上传最新的 @Multipart @POST("house/uploadPhoto") Call<JsonResult> uploadPhoto(@Part("houseCode") String houseCode,@PartMap Map<String, RequestBody> imgs1);
后台使用java的ssm框架搞的,代码如下:
/**
* 上传文件
*
* @param session
* @param file
* @return
*
*/
@RequestMapping(value = "/house/uploadPhoto", produces"text/html;charset=UTF-8")
@ResponseBody
public String fileUpload(HttpSession session,
@RequestParam(value = "file", required = true) MultipartFile[] multiFiles,
String houseCode) {
for (MultipartFile multipartFile : multiFiles) {
String path = Property.getProperty("fileUpload.path");
File dir = new File(path);
if (!dir.exists()) {
dir.mkdir();
}
String filename = "";
if (!multipartFile .isEmpty()) {
// log.debug(session.getServletContext().getRealPath(path));
try {
// 用UUID而不是时间戳
filename = UUID.randomUUID().toString() + "____" +
multipartFile .getOriginalFilename();
// 文件保存路径
String filePath = path + "/" + filename;
// 转存文件
multipartFile .transferTo(new File(filePath));
} catch (Exception e) {
log.error(CustomStringUtils.getExceptionInfo(e));
return JSONUtil.toJsonString(new JsonResult(-1, "上传发生错误!", null));
}
}
}
return JSONUtil.toJsonString(new JsonResult(1, "上传成功!", null));
}
记得在springMvc.xml文件中配置上传的设置:
<!-- 上传文件bean -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
<property name="maxUploadSize"
value="10485760000" /> <property name="maxInMemorySize" value="40960" />
</bean>
切记Android中的
photos.put("file\"
要和RequestParam中的value的值一样。
相关文章推荐
- 实现android上传多张图片和文字给php后台
- Android端使用retrofit上传多张图片,后端使用Php接收图片
- HTML5上传图片,后台使用java
- 使用lrz压缩上传图片,后台使用java
- JAVA 使用springMVC 上传多张图片或文件,并对图片进行按比例缩放处理
- Android Retrofit 实现文字(参数)和多张图片一起上传
- 使用httpHttpURLConnection上传多张图片和文字的方法
- 使用Servlet上传多张图片——Service层(ProductInfoService.java和ProductInfoServiceImpl)
- Android Retrofit 实现(图文上传)文字(参数)和多张图片一起上传
- 使用Servlet上传多张图片——Servlet层(ProductServlet.java)
- Android使用Retrofit仿微信多张图片拍照上传
- iOS使用http post多图片上传带描述+java后台服务器,简单实现例子.
- 使用Servlet上传多张图片——Dao层(BaseDao.java)
- 微信小程序上传图片到服务器(java后台以及使用springmvc)
- 使用Servlet上传多张图片——实体层(ProductInfo.java)
- Android Retrofit 实现文字(参数)和多张图片一起上传
- Android使用Retrofit技术仿微信图片上传,可以选择多张图片拍照上传
- 使用Servlet上传多张图片——Dao层(ProductInfoDao.java)
- java使用FileReader上传图片,可支持预览,支持多张上传
- 使用FileUpload控件上传图片并自动生成缩略图、自动生成带文字和图片的水印图