Spring Boot访问外部HTML

156 阅读1分钟

题记

这是一个看起来很简单的事情,但是遇到坑就很难解决了....

直接上代码

有很多办法,我喜欢在代码里搞

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.StandardCharsets;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
//        registry.addResourceHandler("/uploadFile/**").addResourceLocations("file:C:/Users/22022183/Desktop/");
        registry.addResourceHandler("/html/**").addResourceLocations("file:/ft-order-web/src/main/resources/html2pdf/");
    }

    @Bean
    public HttpMessageConverter<String> responseBody(){
        return new StringHttpMessageConverter(StandardCharsets.UTF_8);
    }
}

然后直接访问,就乱码了

http://ip:port/html/1660099787730/orderPage.html

处理乱码的问题我处理的仨小时

其实也没啥,就是HTML当中少个头

<meta charset="UTF-8"/>

当然,也在配置文件中加了UTF-8的配置,在前面不管用的情况下再考虑下面的配置

spring:
  application:
    name: ft-order
  http:
    encoding:
      charset: UTF-8
      enabled: true
      force: true
  messages:
    encoding: UTF-8
    
server:
  tomcat:
    uri-encoding: UTF-8