这个是只接受一个图片。 小程序 上传图片 的接口 就是这个。一次一个。
/**
* 文件 上传,
* /api/xcx/uploadimage
* @param file
* //自定义上传图片的名字为userId.jpg
//String fileName = request.getAttribute("userId") + "123456.jpg";
*/
@ResponseBody
@RequestMapping("/api/xcx/uploadimage")
public JSONObject uploadimage(HttpServletRequest request)throws Exception {
JSONObject result = new JSONObject();
//获取文件需要上传到的路径
String webPath=request.getServletContext().getRealPath("");
String filePath= "/static/xcx_upload/image/"+DateUtil.formatDate(new Date(), "yyyyMMdd")+"/";
FileUtil.makeDirs(webPath+filePath);
request.setCharacterEncoding("utf-8"); //设置编码
try {
StandardMultipartHttpServletRequest req = (StandardMultipartHttpServletRequest) request;
Iterator<String> iterator = req.getFileNames();
while (iterator.hasNext()) {
HashMap<String, Object> res = new HashMap<String, Object>();
MultipartFile file = req.getFile(iterator.next());
// 获取文件名
String fileNames = file.getOriginalFilename();
int split = fileNames.lastIndexOf(".");
//获取上传文件的后缀
String extName = fileNames.substring(split + 1, fileNames.length());
//申明UUID
String uuid = UUID.randomUUID().toString().replace("-", "");
//组成新的图片名称
String newName = uuid + "." + extName;
System.out.println(newName);
String destPath = webPath+filePath + newName;
//真正写到磁盘上
File file1 = new File(destPath);
OutputStream out = new FileOutputStream(file1);
out.write(file.getBytes());
res.put("url", destPath);
result.put("url", filePath+ newName);
out.close();
}
} catch (Exception e) {
}
return result;
}如果 一次接受多个图片这样设计 android app的 上传图片接口就是用的这个。 一次可以选择9个图片。
/**
* 文件 上传,
* /api/uploadimage
* @param file
* //自定义上传图片的名字为userId.jpg
//String fileName = request.getAttribute("userId") + "123456.jpg";
*/
@ResponseBody
@RequestMapping("/api/uploadimage")
public JSONObject uploadimage(HttpServletRequest request)throws Exception {
JSONObject result = new JSONObject();
//获取文件需要上传到的路径
String webPath=request.getServletContext().getRealPath("");
String filePath= "/static/xcx_upload/image/"+DateUtil.formatDate(new Date(), "yyyyMMdd")+"/";
FileUtil.makeDirs(webPath+filePath);
request.setCharacterEncoding("utf-8"); //设置编码
try {
StandardMultipartHttpServletRequest req = (StandardMultipartHttpServletRequest) request;
Iterator<String> iterator = req.getFileNames();
//String fileName = (String) req.getAttribute("bianhao") ;
while (iterator.hasNext()) {
//拿到所有图片
List<MultipartFile> filelist = req.getFiles(iterator.next());
for(MultipartFile file :filelist ) {
// 获取文件名
String fileNames = file.getOriginalFilename();
int split = fileNames.lastIndexOf(".");
//获取上传文件的后缀
String extName = fileNames.substring(split + 1, fileNames.length());
//申明UUID
String uuid = UUID.randomUUID().toString().replace("-", "");
//组成新的图片名称
String newName = uuid + "." + extName;
System.out.println(newName);
String destPath = webPath+filePath + newName;
//真正写到磁盘上
File file1 = new File(destPath);
OutputStream out = new FileOutputStream(file1);
out.write(file.getBytes());
out.close();
}
}
} catch (Exception e) {
}
return result;
}
站长微信:xiaomao0055
站长QQ:14496453