需要java这边提供一个接口,并把android那边的地址和图片传过来,用java提供的接口把android上传过来的图片,把地址保存到服务器上再把地址保存数据库中, 谢谢!! 急用!
java代码怎么写?
有两种方法,第一,把你的图片转成字节流,然后用post方法把字节流传到服务端,然后服务端接收到字节流之后,开启一个线程把它重新压缩成图片,保存在某个文件夹下面。
第二,开启一个线程,用socket直接把图片放到stream中传到服务端,服务端接收后保存到文件夹下。
第二,开启一个线程,用socket直接把图片放到stream中传到服务端,服务端接收后保存到文件夹下。
温馨提示:答案为网友推荐,仅供参考
第1个回答 推荐于2017-10-03
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String upload(@RequestParam("upload") MultipartFile file,
@RequestParam("tableName") String tableName,
@RequestParam("colName") String colName,
HttpServletRequest request, ModelMap model) {
// System.out.println(imageName);
String path = request.getSession().getServletContext().getRealPath("tables");
// System.out.println(path);
path += "\\"+tableName + "\\" + colName + "\\";
String fileName = file.getOriginalFilename();
System.out.println(path);
System.out.println(fileName);
File targetFile = new File(path, fileName);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
// 保存
try {
file.transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
}
model.addAttribute("fileUrl", request.getContextPath() + "/upload/"
+ fileName);
return "result";
}
自己改改吧。本回答被提问者采纳
public String upload(@RequestParam("upload") MultipartFile file,
@RequestParam("tableName") String tableName,
@RequestParam("colName") String colName,
HttpServletRequest request, ModelMap model) {
// System.out.println(imageName);
String path = request.getSession().getServletContext().getRealPath("tables");
// System.out.println(path);
path += "\\"+tableName + "\\" + colName + "\\";
String fileName = file.getOriginalFilename();
System.out.println(path);
System.out.println(fileName);
File targetFile = new File(path, fileName);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
// 保存
try {
file.transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
}
model.addAttribute("fileUrl", request.getContextPath() + "/upload/"
+ fileName);
return "result";
}
自己改改吧。本回答被提问者采纳