存在问题:以前的老项目打的是war包,tomcat启动解压以后放图片的文件夹可以放在项目里。通过域名加路径的形式可以访问到,但是现在使用springBoot打成jar包,内置tomcat,通过命令运行的形式导致无法在项目中放置存放图片的文件夹
于是我们就使用动态在jar包外生成文件夹,将图片上传到外部文件夹中
不多废话,上代码:
用到的一些服务器(域名用你自己的)上的路径
//服务器访问位置 (服务器上获取图片接口)
public static final String path = "http://m.nongjike.cn/appManager/img/getImg";
//服务器存储图片位置(绝对路径)
public static final String imgpath = "D:/appManager/file/image/upload";
//生产环境默认图片
public static final String localpath = "http://m.nongjike.cn/appManager/img/getImg/start.jpg";
1.首先对启动类做一些修改,主要是实现WebMvcConfigurer ,赋予外部文件夹读写权限
package com.mtnz.njk;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@SpringBootApplication
@EnableScheduling // 开启定时任务
@EnableTransactionManagement // 开启事务支持
// @EnableEurekaServer
// @EnableDiscoveryClient //启用服务注册与发现
// @EnableFeignClients //启用Feign客户端
@EnableCaching // 开启缓存
public class AppManager extends SpringBootServletInitializer implements WebMvcConfigurer {
// 在启动类中添加过滤器
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/File/**")
.addResourceLocations("file:File/");
}
public static void main(String[] args) {
SpringApplication.run(AppManager.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(AppManager.class);
}
}
2.上传图片代码,此处是service层主要代码
/**
* 保存图片
*
* @param file
* @param request
* @return
*/
private static String UPLOAD_PATH = "File/image/upload";
@Transactional
public ResponseResult saveImg(MultipartFile file, HttpServletRequest request) throws Exception {
if (file != null && !"".equals(file)) {
String name = file.getOriginalFilename();
System.out.println(name);
InputStream inputStream = file.getInputStream();
Path directory = Paths.get(UPLOAD_PATH);
if (!Files.exists(directory)) {
Files.createDirectories(directory);
}
Files.copy(inputStream, directory.resolve(name));
// 将信息放入数据库中
String id = IdUtil.simpleUUID();
StartImgEntity startImgEntity = new StartImgEntity();
startImgEntity.setStartImgId(id);
startImgEntity.setStartImgName(name);
startImgEntity.setCreateTime(DateUtil.now());
startImgEntity.setStartImgUrl(FileUtil.path + "/" + name);
int i = startImgManageMapper.insertSelective(startImgEntity);
if (i > 0)
return new ResponseResult(ResponseMessage.OK);
return new ResponseResult(ResponseMessage.FAIL);
}
return new ResponseResult(ResponseMessage.FAIL);
}
3.读取文件代码
/**
* 读取图片
*
* @param response
* @param name
* @throws Exception
*/
@GetMapping("/getImg/{name}")
public void getImage(HttpServletResponse response, String name) throws Exception {
response.setContentType("image/jpeg;charset=utf-8");
response.setHeader("Content-Disposition", "inline; filename=girls.png");
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(Files.readAllBytes(Paths.get(UPLOAD_PATH).resolve(name)));
outputStream.flush();
outputStream.close();
}
4.项目打成jar包,放在服务器上,执行java -jar 命令,一通操作之后,访问上传图片接口saveImg
上传成功,数据库中新增一条信息
调用新增信息的url,成功读取图片