问题描述:
- 【场景】:SpringBoot集成FastDFS上传小视频
- 【技术】:SpringBoot+阿里云+Centos8+FastDFS
- 【说明】:项目采用阿里云ESC搭建FastDFS
- 【问题描述】:SpringBoot集成FastDFS后上传小视频报错
- 【说明】:我只截了其中一部分错误信息,但是这个错误信息包含两部分。
- 【错误信息】:
- 【第一部分】:
@Override java.net.SocketException: Connection reset by peer: socket write error at java.net.SocketOutputStream.socketWrite0(Native Method) ~[na:1.8.0_221] at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111) ~[na:1.8.0_221] at java.net.SocketOutputStream.write(SocketOutputStream.java:155) ~[na:1.8.0_221] at com.github.tobato.fastdfs.domain.proto.AbstractFdfsCommand.sendFileContent(AbstractFdfsCommand.java:131) ~[fastdfs-client-1.26.7.jar:1.26.7] at com.github.tobato.fastdfs.domain.proto.AbstractFdfsCommand.send(AbstractFdfsCommand.java:89) ~[fastdfs-client-1.26.7.jar:1.26.7] at com.github.tobato.fastdfs.domain.proto.AbstractFdfsCommand.execute(AbstractFdfsCommand.java:43) ~[fastdfs-client-1.26.7.jar:1.26.7] at com.github.tobato.fastdfs.domain.conn.ConnectionManager.execute(ConnectionManager.java:81) [fastdfs-client-1.26.7.jar:1.26.7] at com.github.tobato.fastdfs.domain.conn.ConnectionManager.executeFdfsCmd(ConnectionManager.java:65) [fastdfs-client-1.26.7.jar:1.26.7] at com.github.tobato.fastdfs.service.DefaultFastFileStorageClient.uploadFileAndMetaData(DefaultFastFileStorageClient.java:223) [fastdfs-client-1.26.7.jar:1.26.7] at com.github.tobato.fastdfs.service.DefaultFastFileStorageClient.uploadFile(DefaultFastFileStorageClient.java:112) [fastdfs-client-1.26.7.jar:1.26.7] at com.github.tobato.fastdfs.service.DefaultFastFileStorageClient.uploadFile(DefaultFastFileStorageClient.java:68) [fastdfs-client-1.26.7.jar:1.26.7] at com.tanhua.server.service.VideoService.saveVideo(VideoService.java:56) [classes/:na] at com.tanhua.server.controller.VideoController.saveVideo(VideoController.java:34) [classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_221] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_221] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_221] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_221] at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) [spring-web-5.1.19.RELEASE.jar:5.1.19.RELEASE] at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) [spring-web-5.1.19.RELEASE.jar:5.1.19.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) [spring-webmvc-5.1.19.RELEASE.jar:5.1.19.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892) [spring-webmvc-5.1.19.RELEASE.jar:5.1.19.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) [spring-webmvc-5.1.19.RELEASE.jar:5.1.19.RELEASE] at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) [spring-webmvc-5.1.19.RELEASE.jar:5.1.19.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) [spring-webmvc-5.1.19.RELEASE.jar:5.1.19.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) [spring-webmvc-5.1.19.RELEASE.jar:5.1.19.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) [spring-webmvc-5.1.19.RELEASE.jar:5.1.19.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) [spring-webmvc-5.1.19.RELEASE.jar:5.1.19.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:652) [tomcat-embed-core-9.0.39.jar:4.0.FR] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) [spring-webmvc-5.1.19.RELEASE.jar:5.1.19.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) [tomcat-embed-core-9.0.39.jar:4.0.FR]
- 【第二部分】:
com.github.tobato.fastdfs.exception.FdfsIOException: 客户端连接服务端出现了io异常:socket io exception occured while sending cmd at com.github.tobato.fastdfs.domain.proto.AbstractFdfsCommand.execute(AbstractFdfsCommand.java:46) ~[fastdfs-client-1.26.7.jar:1.26.7] at com.github.tobato.fastdfs.domain.conn.ConnectionManager.execute(ConnectionManager.java:81) [fastdfs-client-1.26.7.jar:1.26.7] at com.github.tobato.fastdfs.domain.conn.ConnectionManager.executeFdfsCmd(ConnectionManager.java:65) [fastdfs-client-1.26.7.jar:1.26.7] at com.github.tobato.fastdfs.service.DefaultFastFileStorageClient.uploadFileAndMetaData(DefaultFastFileStorageClient.java:223) [fastdfs-client-1.26.7.jar:1.26.7] at com.github.tobato.fastdfs.service.DefaultFastFileStorageClient.uploadFile(DefaultFastFileStorageClient.java:112) [fastdfs-client-1.26.7.jar:1.26.7] at com.github.tobato.fastdfs.service.DefaultFastFileStorageClient.uploadFile(DefaultFastFileStorageClient.java:68) [fastdfs-client-1.26.7.jar:1.26.7] at com.tanhua.server.service.VideoService.saveVideo(VideoService.java:56) [classes/:na] at com.tanhua.server.controller.VideoController.saveVideo(VideoController.java:34) [classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_221] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_221] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_221] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_221] at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) [spring-web-5.1.19.RELEASE.jar:5.1.19.RELEASE] at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) [spring-web-5.1.19.RELEASE.jar:5.1.19.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) [spring-webmvc-5.1.19.RELEASE.jar:5.1.19.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892) [spring-webmvc-5.1.19.RELEASE.jar:5.1.19.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) [spring-webmvc-5.1.19.RELEASE.jar:5.1.19.RELEASE] at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) [spring-webmvc-5.1.19.RELEASE.jar:5.1.19.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) [spring-webmvc-5.1.19.RELEASE.jar:5.1.19.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) [spring-webmvc-5.1.19.RELEASE.jar:5.1.19.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) [spring-webmvc-5.1.19.RELEASE.jar:5.1.19.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) [spring-webmvc-5.1.19.RELEASE.jar:5.1.19.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:652) [tomcat-embed-core-9.0.39.jar:4.0.FR] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) [spring-webmvc-5.1.19.RELEASE.jar:5.1.19.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) [tomcat-embed-core-9.0.39.jar:4.0.FR]
- 【错误截图】:
错误分析:
- 【说明】:当时百度错误信息时,网上大多的修改方法是,关闭防火墙 ,端口和权限,但是我认为做这方面的应该不会犯不关防火墙这么简单的错误,而且之前我写了一个测试方法,上传一张简单的照片是可以成功。因此错误应该不在防火墙方面。
- 【分析】:在第二错误信息中我发现,它提示exception occured while sending cmd,也就是在发送cmd命令时发生的异常,因此我联想到可能是上传的视频过大或过长,导致连接超时抛出的错误。
- 【测试】:我用测试代码进行测试了一下,上传成功。
- 【测试代码】:
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.File;
import java.io.IOException;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestFastDFS {
@Autowired
protected FastFileStorageClient storageClient;
@Test
public void testUpload() {
// String path = "C:\Users\Administrator\Desktop\smartvideo\11111.mp4";
String path = "F:\yyqx.jpg";
File file = new File(path);
try {
// M00/00/00/rBFdsWB9bPyAFl0zADKAAKO0xN8965.mp4
StorePath storePath = this.storageClient.uploadFile(FileUtils.openInputStream(file), file.length(), "jpg", null);
//M00/00/00/rBFdsWB69cSAO8jkAAAcvkXRfHk190.jpg
System.out.println(storePath);
//group1/M00/00/00/wKgfUV2GJSuAOUd_AAHnjh7KpOc1 .1.jpg
System.out.println(storePath.getFullPath());
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 【说明】:我先上传了一张图片,上传成功,紧接着我将报错的视频赋值到地址中进行测试。出现和上面相同的错误,然后我进行Debug打断点进行查看,发现在读取视频时,视频的路径为乱码,因此我联想到可能是中文路径的原因,因为我一开始将小视频存在桌面上一个中文的文件夹下。因此导致错误。
代码及配置:
- 【上传代码】
public Boolean saveVideo(MultipartFile picFile, MultipartFile videoFile) {
User user = UserThreadLocal.get();
Video video = new Video();
video.setUserId(user.getId());
// 设置所有可见
video.setSeeType(1);
try {
// 上传图片
PicUploadResult picUploadResult = this.picUploadService.upload(picFile);
video.setPicUrl(picUploadResult.getName());
// 上传视频
StorePath storePath = storageClient.uploadFile(videoFile.getInputStream(), videoFile.getSize(), StringUtils.substringAfter(videoFile.getOriginalFilename(), "."), null);
// 配置URL
video.setVideoUrl(fdfsWebServer.getWebServerUrl() + storePath.getFullPath());
return this.videoApi.saveVideo(video);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
- 【配置信息-application.properties】:
# =================================================================== # 分布式文件系统FDFS配置 # =================================================================== fdfs.so-timeout=15001 fdfs.connect-timeout=60000 #缩略图生成参数 fdfs.thumb-image.width=150 fdfs.thumb-image.height=150 #TrackerList参数,支持多个 fdfs.tracker-list=外网IP:22122 #配置FastDFS访问前缀 fdfs.web-server-url=http://外网IP:8888/原因分析:
- 【分析】:通过第二个错误信息发现应该是在上传视频写入时出现错误,但是可以肯定的是连接超时造成的,同时通过Debug断点发现,读取的中文路径全是乱码,因此分析可能是因为中文路径读取不到造成的。
解决方案:
- 【解决方案】:将图片存到英文或数字的路径下即可。



