使用套接字发送文件时出现问题。问题是在目标主机上,我发送的文件被复制到第一个文件的内容(包括文件名和其他文件的内容)而不是单独的文件。对于我的测试用例,我只发送两个文件。
本地主持人:
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();
}
目标主持人:
public void escucha(int puerto) throws IOException {
ServerSocket server = null;
Socket connection;
BufferedInputStream bis;
BufferedOutputStream bos;
byte[] receivedData;
int in;
String file;
try {
server = new ServerSocket(puerto);
while (true) {
connection = server.accept();
receivedData = new byte[1024];
DataInputStream dis = new DataInputStream(
connection.getInputStream());
// recibimos la cantidad de ficheros que recibiremos
int q = dis.readInt();
for (int i = 0; i < q; i++) {
bis = new BufferedInputStream(connection.getInputStream());
file = dis.readUTF();
file = "C:\\targetFolder" + File.separator
+ file.substring(file.indexOf('\\') + 1, file.length());
bos = new BufferedOutputStream(new FileOutputStream(file));
while ((in = bis.read(receivedData)) != -1) {
bos.write(receivedData, 0, in);
}
bos.flush();
}
dis.close();
server.close();
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
server.close();
}
}
在for的第二次迭代中发生:java.io.EOFException
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)
答案 0 :(得分:1)
很抱歉,但我猜你错过了设计和代码方面的一些方面。首先,你需要协议,第二,你需要在目标端的单一输入流源。
在本地输出流中,我建议......
因此输出流将类似于:
[num_of_files][file1_name_length][file1_name][file1_size][file1_data][file2_.....]
我建议打开 ONE 输入流 ONLY 并根据协议阅读。请记住在每个循环中刷新和关闭每个FileOutputStream。