美文网首页
SSM实现文件的上传

SSM实现文件的上传

作者: MONEY的奴隶 | 来源:发表于2019-11-22 13:38 被阅读0次

@RequestMapping(value = "/upload", method = RequestMethod.POST)

@ResponseBody

public Map<String, Object> upload(MultipartFile file,HttpServletRequest request) throws IOException {

Map<String, Object> dataMap = new HashMap<>();

//该系统默认分隔符

String string = File.separator;

//系统服务器路径

String bath = request.getSession().getServletContext().getRealPath(string);

//获取文件名称

String originalFilename = file.getOriginalFilename();

//准备拼接

StringBuilder stringBuilder = new StringBuilder();

stringBuilder.append(bath);

stringBuilder.append("resources");

stringBuilder.append(string);

stringBuilder.append("img");

stringBuilder.append(string);

stringBuilder.append(UUID.randomUUID().toString().replace("-", ""));

stringBuilder.append(originalFilename.substring(originalFilename.lastIndexOf(".")));

File file2 = new File(stringBuilder.toString());

if (!file2.exists()) {

file2.createNewFile();

}

//进行文件的保存

file.transferTo(file2);

//返回文件的绝对路径

dataMap.put("imgUrl2", file2.getAbsolutePath());

return dataMap;

}

相关文章

网友评论

      本文标题:SSM实现文件的上传

      本文链接:https://www.haomeiwen.com/subject/ylxnaftx.html