Просмотр исходного кода

fix:解决linux下文件名超过255字节的问题v1

tujidelv 3 лет назад
Родитель
Сommit
d956ee2be2

+ 6 - 0
pom.xml

@@ -106,6 +106,12 @@
             <artifactId>lombok</artifactId>
             <version>1.16.20</version>
         </dependency>
+        <!-- 单元测试 -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
         <!--fastjson-->
         <dependency>
             <groupId>com.alibaba</groupId>

+ 7 - 0
src/main/java/top/lvzhiqiang/mapper/VideoInfoPoolMapper.java

@@ -69,4 +69,11 @@ public interface VideoInfoPoolMapper {
     List<VideoCast> findCast();
 
     List<VideoInfo> getVideoInfoPoolList(Map<String, Object> params);
+
+
+    /**
+     * 查询所有
+     */
+    @Select("SELECT * FROM video_info_pool WHERE delete_flag = 1")
+    List<VideoInfoPool> findAll();
 }

+ 11 - 1
src/main/java/top/lvzhiqiang/service/impl/BgServiceImpl.java

@@ -22,6 +22,7 @@ import top.lvzhiqiang.util.StringUtils;
 
 import javax.annotation.Resource;
 import java.io.*;
+import java.nio.charset.StandardCharsets;
 import java.time.Instant;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
@@ -328,7 +329,16 @@ public class BgServiceImpl implements BgService {
 
         long start = System.currentTimeMillis();
         Connection.Response response = Jsoup.connect(href).method(Connection.Method.GET).ignoreContentType(true).timeout(50 * 1000).execute();
-        String fileName = issueDate.concat(" ").concat(h3).concat(".jpg");
+
+        String fileName = issueDate.concat(" ").concat(h3);
+        byte[] imgUrlBytes = fileName.getBytes(StandardCharsets.UTF_8);
+        if (imgUrlBytes.length > 251) {
+            byte[] imgUrlDestBytes = new byte[251];
+            System.arraycopy(imgUrlBytes, 0, imgUrlDestBytes, 0, 251);
+            fileName = new String(imgUrlDestBytes, StandardCharsets.UTF_8).replace("�","");
+        }
+        fileName = fileName.concat(".jpg");
+
         saveFile(response.bodyStream(), machiPath.concat(fileName));
         long end = System.currentTimeMillis();
 

+ 48 - 0
src/test/java/top/lvzhiqiang/Test6.java

@@ -0,0 +1,48 @@
+package top.lvzhiqiang;
+
+import lombok.extern.slf4j.Slf4j;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import top.lvzhiqiang.entity.VideoInfoPool;
+import top.lvzhiqiang.mapper.VideoInfoPoolMapper;
+
+import javax.annotation.Resource;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+
+/**
+ * 单元测试类
+ *
+ * @author lvzhiqiang
+ * @since 11:19 2022/5/2
+ */
+@Slf4j
+@RunWith(SpringJUnit4ClassRunner.class)
+@SpringBootTest(properties = {
+        "spring.profiles.active=dev",
+        "logging.level.top.lvzhiqiang=DEBUG"
+}
+)
+public class Test6 {
+
+    @Resource
+    private VideoInfoPoolMapper videoInfoPoolMapper;
+
+    @Test
+    public void testGetBusinessDeptSummary() {
+        List<VideoInfoPool> videoInfoPoolList = videoInfoPoolMapper.findAll();
+        byte[] imgUrlDestBytes = new byte[251];
+        for (VideoInfoPool videoInfoPool : videoInfoPoolList) {
+            String imgUrl = videoInfoPool.getImgUrl();
+            byte[] imgUrlBytes = imgUrl.getBytes(StandardCharsets.UTF_8);
+            if (imgUrlBytes.length > 251) {
+                System.out.println(imgUrl);
+                System.arraycopy(imgUrlBytes, 0, imgUrlDestBytes, 0, 251);
+                System.out.println(new String(imgUrlDestBytes, StandardCharsets.UTF_8).replace("�", ""));
+                System.out.println("========================");
+            }
+        }
+    }
+}