Tag Archives: request

[Solved] urllib.error.URLError: <urlopen error [SSL: WRONG_VERSION_NUMBER] wrong version number

There are totally four methods to solve this error

 

Solution:
Method 1: the SSL certificate problem
you can open the URL with the following code

import ssl
 
# This restores the same behavior as before.
context = ssl._create_unverified_context()
response = urllib.request.urlopen("https://no-valid-cert", context=context)

https://no-valid-cert you can change it to the website you want

Method 2: Change https to http, because some versions of python verify the SSL certificate once when you urllib.urlopen an https.

Method 3: Add the following codes:

import ssl
ssl._create_default_https_context = ssl._create_unverified_context

I just add it to the py file when calling commands from the urllib library that comes with python 3.8.0

model = ALBEF(config=config, text_encoder=args.text_encoder, tokenizer=tokenizer, init_deit=True)

That is, when the model is initialized (init_deit), the error occurs when calling return self.sslsocket_class._create in lib/python3.8/http/client.py under python 3.8, but at the beginning of the py file for initializing the model Add these two lines and you’ll be fine

Method 4:
Upgrade your python interpreter version, e.g. 2.7 or 3.7 to 3.8 or even 3.9

Tencent cloud container easyexcel export excel error NoClassDefFoundError: could not initialize class sun.awt.x11fontmanager

Development environment:

springboot 2.4.3easyexcel 2.2.7jdk8


Problem Description:

1. There is no local problem. An error was reported after the release of Tencent cloud container.

        EasyExcel.write(response.getOutputStream(), ViewDatass.class).sheet("xxx明细").doWrite(datas);

2. Error report log

[Request processing failed; nested exception is com.alibaba.excel.exception.ExcelGenerateException: java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11FontManager] with root cause
java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11FontManager
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:348)
        at sun.font.FontManagerFactory$1.run(FontManagerFactory.java:82)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:74)
        at java.awt.Font.getFont2D(Font.java:491)
        at java.awt.Font.canDisplayUpTo(Font.java:2060)
        at java.awt.font.TextLayout.singleFont(TextLayout.java:470)
        at java.awt.font.TextLayout.<init>(TextLayout.java:531)
        at org.apache.poi.ss.util.SheetUtil.getDefaultCharWidth(SheetUtil.java:275)
        at org.apache.poi.xssf.streaming.AutoSizeColumnTracker.<init>(AutoSizeColumnTracker.java:117)
        at org.apache.poi.xssf.streaming.SXSSFSheet.<init>(SXSSFSheet.java:82)
        at org.apache.poi.xssf.streaming.SXSSFWorkbook.createAndRegisterSXSSFSheet(SXSSFWorkbook.java:658)
        at org.apache.poi.xssf.streaming.SXSSFWorkbook.createSheet(SXSSFWorkbook.java:679)
        at org.apache.poi.xssf.streaming.SXSSFWorkbook.createSheet(SXSSFWorkbook.java:90)
        at com.alibaba.excel.util.WorkBookUtil.createSheet(WorkBookUtil.java:66)
        at com.alibaba.excel.context.WriteContextImpl.createSheet(WriteContextImpl.java:205)
        at com.alibaba.excel.context.WriteContextImpl.initSheet(WriteContextImpl.java:185)
        at com.alibaba.excel.context.WriteContextImpl.currentSheet(WriteContextImpl.java:122)
        at com.alibaba.excel.write.ExcelBuilderImpl.addContent(ExcelBuilderImpl.java:53)
        at com.alibaba.excel.ExcelWriter.write(ExcelWriter.java:161)
        at com.alibaba.excel.ExcelWriter.write(ExcelWriter.java:146)
        at com.alibaba.excel.write.builder.ExcelWriterSheetBuilder.doWrite(ExcelWriterSheetBuilder.java:61)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197)
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141)
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1060)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:962)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:626)
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)

Cause analysis:

There is no local error report. When you publish it to Tencent cloud container, you can report an error. If you look at the prompt, it should be a certain font. As a result, Excel can’t render it. Interestingly, there are several errors reported in the middle, which leads to many detours…

com.alibaba.excel.exception.ExcelGenerateException: java.lang.UnsatisfiedLinkError: /usr/local/java/xxx/jre/lib/amd64/libfontmanager.so: libfreetype.so.6: cannot open shared object file: No such file or directory

Solution:

The problem is solved by adding fonts to the docker file.

# Add font
RUN yum install dejavu-sans-fonts fontconfig -y