springboot之图片上传和访问
2018-03-09 15:52
323 查看
直接上代码再说@RestController
@RequestMapping(value = "/upload")
public class UploadImgController {
private static final Logger LOGGER = LoggerFactory.getLogger(UploadImgController.class);
/**支持图片的类型**/
private String [] types={".jpg",".bmp",".jpeg",".png"};
private final String PATH = "img";
/**
* 上传文件
* @param file
* @return
* @throws Exception
*/
@RequestMapping(value = "/image",method = RequestMethod.POST)
public String upload(@RequestParam(value = "file",required = false) MultipartFile file) throws Exception {
RespInfo respInfo = new RespInfo();
String fileName = "";
if (!file.isEmpty()) {
fileName = file.getOriginalFilename();
String type = fileName.substring(fileName.lastIndexOf("."));
if (Arrays.asList(types).contains(type)){
BufferedOutputStream out = null;
File fileSourcePath = new File(PATH);
if (!fileSourcePath.exists()) {
fileSourcePath.mkdirs();
}
fileName = file.getOriginalFilename();
LOGGER.info("上传的文件名为:" + fileName);
out = new BufferedOutputStream(
new FileOutputStream(new File(fileSourcePath, fileName)));
out.write(file.getBytes());
out.flush();
System.out.println(fileName.toString());
respInfo.setStatus(InfoCode.SUCCESS);
respInfo.setMesssage("上传成功!");
return JSON.toJSONString(respInfo);
}
respInfo.setMesssage("此格式不支持!");
respInfo.setStatus(InfoCode.FAIL);
return JSON.toJSONString(respInfo);
}
respInfo.setMesssage("文件不能为空!");
respInfo.setStatus(InfoCode.FAIL);
return JSON.toJSONString(respInfo);
}
}
我上传到了img下,可以出项目下查看是否有这个文件。
2.文件访问
我使用绝对路径访问/Users/zwd-admin/company/API/manage-life-of-student/img/img1.jpeg
@RequestMapping(value = "/upload")
public class UploadImgController {
private static final Logger LOGGER = LoggerFactory.getLogger(UploadImgController.class);
/**支持图片的类型**/
private String [] types={".jpg",".bmp",".jpeg",".png"};
private final String PATH = "img";
/**
* 上传文件
* @param file
* @return
* @throws Exception
*/
@RequestMapping(value = "/image",method = RequestMethod.POST)
public String upload(@RequestParam(value = "file",required = false) MultipartFile file) throws Exception {
RespInfo respInfo = new RespInfo();
String fileName = "";
if (!file.isEmpty()) {
fileName = file.getOriginalFilename();
String type = fileName.substring(fileName.lastIndexOf("."));
if (Arrays.asList(types).contains(type)){
BufferedOutputStream out = null;
File fileSourcePath = new File(PATH);
if (!fileSourcePath.exists()) {
fileSourcePath.mkdirs();
}
fileName = file.getOriginalFilename();
LOGGER.info("上传的文件名为:" + fileName);
out = new BufferedOutputStream(
new FileOutputStream(new File(fileSourcePath, fileName)));
out.write(file.getBytes());
out.flush();
System.out.println(fileName.toString());
respInfo.setStatus(InfoCode.SUCCESS);
respInfo.setMesssage("上传成功!");
return JSON.toJSONString(respInfo);
}
respInfo.setMesssage("此格式不支持!");
respInfo.setStatus(InfoCode.FAIL);
return JSON.toJSONString(respInfo);
}
respInfo.setMesssage("文件不能为空!");
respInfo.setStatus(InfoCode.FAIL);
return JSON.toJSONString(respInfo);
}
}
我上传到了img下,可以出项目下查看是否有这个文件。
2.文件访问
我使用绝对路径访问/Users/zwd-admin/company/API/manage-life-of-student/img/img1.jpeg
相关文章推荐
- 解除spring-boot上传图片大小的限制
- springboot整合H+,基于summernote的富文本编辑器图片上传
- SpringBoot之访问静态图片
- spring boot实现上传图片并在页面上显示
- spring boot实现图片上传和下载功能
- 前台基于vue 图片上传 后台基于springboot的 图片压缩上传
- SpringBoot限制文件或图片上传大小配置
- Springboot restTempate POST 上传图片
- SpringBoot 学习记录(五)- 文件上传(和图片服务器fastdfs测试)
- spring boot实现图片的上传和下载功能
- springBoot整合ftp上传图片功能
- 详解js插件ajaxupload实现图片上传(适用springboot)
- Spring Boot上传图片
- Spring Boot上传图片与展示
- SpringBoot使用Ckediter上传图片
- springBoot 连接打包成jar包运行时,获取图片上传文件、前端页面等文件
- spring boot实现上传图片并在页面上显示
- 使用 SpringBoot + Ckeditor 富文本编辑器、图片上传
- 关于SpringBoot上传图片的几种方式
- SpringBoot:上传图片到Linux服务器(html+ajax+jquery+ftpclient+nginx)