直播app源代码,SpringBoot 上传图片接口实现的相关代码
yml里参数
menu:
img:
zipPath: /usr/local/img/menu/
zipUrl: http://39.104.123.82:12604/img/menu/
uploadPath: D:/usr/local/img/upload/
uploadUrl: http://39.104.123.82:12604/img/upload/
@Value("${menu.img.uploadPath}")
private String uploadPath;
@Value("${menu.img.uploadUrl}")
private String uploadUrl;
@Value("${menu.img.zipPath}")
private String zipPath;
@Value("${menu.img.zipUrl}")
private String zipUrl;
/**
* 上传图片
* @param IdForm
* @return RestResult<Object>
*/
@ControllerLog
@ResponseBody
@RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
public RestResult<Object> uploadImg(@RequestParam("file") MultipartFile file,String id) {
log.info("Controller层,method[uploadImg]调用开始!");
RestResult<Object> info = menuLayoutService.uploadImg(file,id);
log.info("Controller层,method[uploadImg]调用结束!");
return info;
}
/**
* 上传图片
* @Description:
* @param: MultipartRequest file, HttpServletRequest request, IdForm form
* @return:RestResult<Object>
* @author: zhanghd
* @date: 2018年12月21日 下午2:39:24
*/
@Override
public RestResult<Object> uploadImg(@RequestParam("file") MultipartFile file,String id) {
String oldName = file.getOriginalFilename();
String imgType = oldName.substring(oldName.lastIndexOf("."), oldName.length());
String name = UUID.randomUUID().toString()+imgType; // 图片名
String realpath = uploadPath + "/" + id;
String fileName = writeUploadFile(file, realpath, name);
String url = uploadUrl + fileName;
return RestResult.genOkResult(url);
}
/**
* 文件上传处理
* @Description: 上传图片
* @param:MultipartFile file, String realpath, String fileName
* @return:String fileName
* @author: zhanghd
* @date: 2018年12月21日 下午5:00:56
*/
public static String writeUploadFile(MultipartFile file, String realpath, String fileName) {
File fileDir = new File(realpath);
if (!fileDir.exists())
fileDir.mkdirs();
InputStream input = null;
FileOutputStream fos = null;
try {
input = file.getInputStream();
fos = new FileOutputStream(realpath + "/" + fileName);
IOUtils.copy(input, fos);
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(fos);
}
return fileName;
}
以上就是 直播app源代码,SpringBoot 上传图片接口实现的相关代码,更多内容欢迎关注之后的文章