通过java套接字发送多个文件

通过java套接字发送多个文件,java,sockets,file-transfer,Java,Sockets,File Transfer,使用套接字发送文件时出现问题。问题是,在目标主机上,我发送的文件被复制到第一个文件的内容(包括其他文件的文件名和内容),而不是作为单独的文件。对于我的测试用例,我只发送了两个文件 本地主机: public void sendFilesToServer(List<String> compFiles) throws IOException { Socket client = new Socket("127.0.0.1", 5000); BufferedInputStr

使用套接字发送文件时出现问题。问题是,在目标主机上,我发送的文件被复制到第一个文件的内容(包括其他文件的文件名和内容),而不是作为单独的文件。对于我的测试用例,我只发送了两个文件

本地主机:

public void sendFilesToServer(List<String> compFiles) throws IOException {

    Socket client = new Socket("127.0.0.1", 5000);

    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    int in;
    byte[] byteArray;

    DataOutputStream dos = new DataOutputStream(
            client.getOutputStream());

    //numero de archivos a enviar
    dos.writeInt(2);
    dos.flush();
    bos = new BufferedOutputStream(client.getOutputStream());

    for(String file : compFiles){
        // Fichero a transferir
        final String filename = file;

        try {
            final File localFile = new File(filename);
            bis = new BufferedInputStream(new FileInputStream(localFile));              
            dos.writeUTF(localFile.getName());
            dos.flush();
            byteArray = new byte[8192];
            while ((in = bis.read(byteArray)) != -1) {
                bos.write(byteArray, 0, in);
            }

            bos.flush();

        } catch (Exception e) {
            System.err.println(e);
        }
    }
    dos.close();
    bos.close();
    client.close();

}

很抱歉,我猜您在设计和代码方面遗漏了很多方面。首先,您需要协议,其次,您需要在目标端提供单一的输入流源

协议 在本地端的输出流中,我建议

  • 要发送的文件数
  • 文件名的长度
  • 文件名
  • 文件的长度
  • 文件内容
  • 如果有,对下一个文件重复2~5次
  • 因此,输出流类似于:

    [num_of_files][file1_name_length][file1_name][file1_size][file1_data][file2_.....]
    
    目标方
    我建议只打开一个输入流,并按照协议阅读。请记住刷新并关闭每个循环中的每个FileOutputStream。

    您可以发布您的
    EOFEException的完整堆栈跟踪吗?
    ?我考虑了您的建议,我可以解决我的问题,谢谢Kaayam。
    12:54:19,723 ERROR [stderr] (http-localhost-127.0.0.1-8082-6) java.io.EOFException
    12:54:19,724 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:340)
    
    12:54:19,725 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at java.io.DataInputStream.readUTF(DataInputStream.java:589)
    
    12:54:19,726 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at java.io.DataInputStream.readUTF(DataInputStream.java:564)
    
    12:54:19,727 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.tfc.websvn.transfer.socket.recibefichero.ServidorRecibeFichero.escucha(ServidorRecibeFichero.java:44)
    
    12:54:19,728 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.tfc.websvn.transfer.servlet.InitSocketServlet.doGet(InitSocketServlet.java:41)
    
    12:54:19,728 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
    
    12:54:19,729 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
    
    12:54:19,730 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)
    
    12:54:19,731 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
    
    12:54:19,732 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    
    12:54:19,733 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    
    12:54:19,734 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    
    12:54:19,735 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    
    12:54:19,736 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    
    12:54:19,737 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    
    12:54:19,739 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    
    12:54:19,739 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    
    12:54:19,740 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    
    12:54:19,742 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    
    12:54:19,743 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
    
    12:54:19,744 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    
    12:54:19,745 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    
    12:54:19,746 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    
    12:54:19,747 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
    
    12:54:19,748 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    
    12:54:19,749 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
    
    12:54:19,750 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    
    12:54:19,751 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
    
    12:54:19,752 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    
    12:54:19,753 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
    
    12:54:19,754 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    
    12:54:19,755 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    
    12:54:19,755 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125)
    
    12:54:19,756 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    
    12:54:19,756 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    
    12:54:19,757 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    
    12:54:19,757 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    
    12:54:19,758 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    
    12:54:19,758 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    
    12:54:19,759 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
    
    12:54:19,759 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)
    
    12:54:19,760 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
    
    12:54:19,760 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)
    
    12:54:19,761 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
    
    12:54:19,761 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:397)
    
    12:54:19,761 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50)
    
    12:54:19,762 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)
    
    12:54:19,763 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
    
    12:54:19,763 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    
    12:54:19,763 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    
    12:54:19,764 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)
    
    12:54:19,764 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
    
    12:54:19,765 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)
    
    12:54:19,765 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)
    
    12:54:19,766 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at java.lang.Thread.run(Thread.java:744)
    
    [num_of_files][file1_name_length][file1_name][file1_size][file1_data][file2_.....]