1.导入已存在的maven项目运行起来404
eclipse有几处设置应该用jdk目录里的jre; server设置里选择tomcat安装目录
2. 导入本地jar包
把这个jar包加在build path 下, 然后还需要在把jar包拷到WEB-INF/lib目录下
3. ssm引入spring task
spring的配置xml 加入
<task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
<task:scheduler id="qbScheduler" pool-size="10"/>
做定时任务的类上加@Component。
@Scheduled的属性有: fixedDelay 上一个任务完成的间隔
fixedRate 上一个任务开始的间隔
4. springboot打包依赖本地项目
eclipse中在build path添加就ok,但需要package时候报错了,项目里之前的pom文件是:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
<includes>
<include>
<!-- 本地项目的groupId artifactId -->
</include>
</includes>
</configuration>
<!-- 省略 execution - repackage<-->
</plugin>
</plugins>
</build>
这样的话只会打包当前项目和include里的本地项目,直接执行jar会报错,必须把项目中依赖的其他jar包拷到lib目录下,执行 java -jar -Dloader.path="./lib" 当前项目package出来的.jar
百度了一下,<configuration>里 layout 必须是 ZIP的时候,用<excludeGroupIds> 可以配置需要忽略的依赖,这样可以起到瘦身的作用,打包更快,但我们这个项目并没有用<excludeGroupIds>啊,4月21日周六加到七点半终于搞明白了,<includes>会在打包的时候忽略所有依赖,只打包他里面配置的!!
讨论了一下,认为这种引入本地jar的方式是错误的,所以修改如下:
把<configuration>里的都去掉了
在<build>里加入
<resources>
<resource>
<directory>lib</directory>
<targetPath>BOOT-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<targetPath>BOOT-INF/classes/</targetPath>
</resource>
</resources>
然后还需要指定一下主类,可以在<properties>里加入
<start-class>springboot 启动类名</start-class>
如上就是在需要引用本地项目jar包情况下的 两种打包方法:单独把别的依赖放在一个目录下;所有依赖都打包
5. spring boot返回本地图片
第一种做法:直接url请求静态文件
@Configuration
public class MvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(拦截的url).addResourceLocations(本地文件路径);
}
}
第二种做法 :
public static String show(String pathAndName, HttpServletResponse response){
File file = new File(pathAndName);
if (file.exists()) {
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// close
}
}
return null;
}
6. 解决图片上传from-data里的乱码 (20180623)
springboot2.0.0下同时提交图片和实体类属性,会乱码,用了一天时间!
查询别人的解决方案:
spring:
http:
encoding:
force: true
charset: utf-8
enabled: true
public class MvcConfig extends WebMvcConfigurerAdapter{
@Bean
public HttpMessageConverter<String> responseBodyConverter() {
StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
return converter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
converters.add(responseBodyConverter());
}
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false);
}
}
但是没解决,发现WebMvcConfigurerAdapter Deprecated, 尝试换成WebMvcConfigurationSupport,乱码解决了但是会出现静态文件404,配置文件中spring.resources.static-locations失效,是因为自动配置类 WebMvcAutoConfiguration只有在缺失WebMvcConfigurationSupport类型的bean时才会生效,所以需要自己重写
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**/**").addResourceLocations("classpath:/static/").addResourceLocations("classpath:/static/")
.addResourceLocations("classpath:/public/").addResourceLocations("classpath:/mapper/").addResourceLocations("file:" + uploadProperties.getPath());
super.addResourceHandlers(registry);
}
整整浪费了一天时间,对spring还是不了解,过段时间用空一定看文档,深入学习!
7. 生成的图片base64后返回给前端(20180806)
生成后可能会有 = 号补位,再给前端等于号会转成unicode导致无法解析图片
String base64Image= "data:image/jpeg;base64," + java.util.Base64.getEncoder().encodeToString(jpegOutputStream.toByteArray());
map.put("img", base64Image.replaceAll("=",""));
8. 上传照片文件
public static String uploadImage(MultipartFile file) throws UploadException {
if (file == null) {
throw new UploadException("上传为空!");
}
String originalName = file.getOriginalFilename();
if(!uploadProperty.getAcceptType().contains( file.getContentType())){
throw new UploadException( originalName + " 类型错误");
}
if(file.getSize() / 1024 /1024 > uploadProperty.getMaxSize()) {
throw new UploadException( originalName + " 超大");
} // String suffix = originalName.substring(originalName.lastIndexOf("."));
String newName = generateImageName(6, originalName.substring(originalName.lastIndexOf(".")));
File saveDir = new File(uploadProperty.getPath() + newName);
try {
file.transferTo(saveDir);
} catch (IOException e) {
e.printStackTrace(); throw new UploadException("保存错误!");
}
System.err.println(uploadProperty.getPath() + newName);
return BaseConstant.SYS_STATIC_ROOT + "/" + newName;
}
调用代码如下
@PostMapping(value="/application")
public String addOne(JobApplication application, @RequestParam(value = "photoImg", required = false) MultipartFile photoImg){
if (photoImg != null){
try {
application.setLinkPhoto(UploadUtil.uploadImage(photoImg));;
} catch (UploadException e) {
e.printStackTrace();
return new JsonResult(null, "保存图片错误!").failure().toString();
}
}
}
9. poi导出父子一对多结构的数据(合并单元格并居中)20180925
正常添加数据
HSSFRow row1 = sheet.createRow(rowNum);
row1.createCell(0).setCellValue(order.getOrderId());
合并单元格,并且居中
private void colspan(HSSFSheet sheet, int no, int beforeRowNum, int count, HSSFCellStyle style){
for (int i = 0; i < no; i++){
CellRangeAddress cra = new CellRangeAddress(beforeRowNum, count, i,i);//合并的起始行结束行起始列结束列
sheet.addMergedRegion(cra);
RegionUtil.setBorderTop(BorderStyle.THICK, cra, sheet);
HSSFCell cell = sheet.getRow(beforeRowNum).getCell(i);
cell.setCellStyle(style);
}
}
10 . Lombok简化spring注入(20180927)
spring注入的三种方式:直接字段,构造方法,set方法。使用 Lombok可以如下简化:
@Service
@Transactional
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ProductService {
//@Autowired
private final ProductMapper productMapper; // 必须是final
public Product getById(String id){
return productMapper.getById(id);
}
}
11 .springboot更新到2.1后数据库配置文件报错
因为2.1把mysql到驱动更新到8.0了。需要加上时区到信息
spring:
datasource:
url: jdbc:mysql://47.93.246.239:3306/redseft?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
网友评论