读书人

[小白]TCP协议文件传输有关问题

发布时间: 2013-06-26 14:29:32 作者: rapoo

[小白]TCP协议文件传输问题
本帖最后由 lrcry 于 2013-03-31 16:16:52 编辑 我想实现一个TCP协议,C/S模式的文件上传,Server支持多线程;
但是不知为什么,我通过客户端上传上来的文件,到达服务器的时候时好时坏;
意思就是,有的时候Server能接收到完整的文件,但是有的时候,接收到的文件就是空的。而且是空的时候多,是完整文件的时候少。
有源码如下:

服务器端


/*Server端,多线程接收*/
public class MainBean {
public static void main(String[] args){
/*Init socket*/
try {
System.out.print("Starting server...");
ServerSocket ss = new ServerSocket(10086);
System.out.println("OK");
while(true){
Socket s = ss.accept();
System.out.println("Get TCP connection in: " + s.getInetAddress());
new Thread(new Threads(s)).start();
}
} catch (IOException e1) {
System.out.println("Server init failed!");
}


服务器线程实现

/*Server端,线程Threads.java*/
public class Threads implements Runnable {
private Socket s;

public Threads(Socket s){
super();
this.s = s;
}

@Override
public void run() {
int count = 0;
String ip = s.getInetAddress().getHostAddress();

try {
InputStream in = s.getInputStream();

System.out.print("Creating files on server...");
File file = new File(ip + "(" + count + ").xml");//命名规律

/*如果文件已经存在,则重命名为如下格式*/
while(file.exists()){
file = new File(ip + "(" + count++ + ").xml");
}
System.out.println("OK");

/*接收*/
System.out.println("Receiving...");
FileOutputStream fOut = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len = 0;
while((len = in.read(buf)) != -1){
fOut.write(buf, 0, len);
}
System.out.println("OK");

/*向客户端返回信息*/
OutputStream out = s.getOutputStream();
System.out.println("Server: returning to client..");
out.write("Upload succeed.".getBytes());

fOut.close();
s.close();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}


客户端,上传的文件时xml格式

/*自己定义的两个异常,可忽略*/
class FileNameException extends Exception{
FileNameException(String msg){
super(msg);
}
}
class FileSizeException extends Exception{
FileSizeException(String msg){
super(msg);
}
}

public class UploadClient {

public static void main(String[] args) throws IOException, FileNameException, FileSizeException, ParserConfigurationException, SAXException, TransformerFactoryConfigurationError, TransformerException {

String fileName = "xmldata3.xml";
File file = new File(fileName);

if(!file.exists() && file.isFile()){


throw new IOException();
}

if(!fileName.endsWith(".xml")){
throw new FileNameException("Wrong file type");
}

if(file.length() > 2*1024*1024){
throw new FileSizeException("Too big file");
}

/* Mar 29, 2013*/
String ipAddress = "127.0.0.1";
int portNum = 10086;
System.out.print("Connecting...");
Socket s = new Socket(ipAddress, portNum);
System.out.println("OK");

System.out.print("Opening file...");
FileInputStream fIn = new FileInputStream(file);//创建已打开文件的文件流
System.out.println("OK");

/*上传文件*/
OutputStream out = s.getOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while((len = fIn.read(buf)) != -1){
out.write(buf, 0, len);
}
System.out.print("Ending writing...");
s.shutdownOutput();//结束文件上传
System.out.println("OK");

/*接收服务器返回信息*/
InputStream in = s.getInputStream();
byte[] bufIn = new byte[1024];
int num = in.read(bufIn);
System.out.println(new String(bufIn, 0, num));

fIn.close();
s.close();
}

}



每次我先启动Server,正常,然后启动Client,上传文件,一切看似正常,打开服务器端接收到的文件,发现文件很多时候都是空的,有完整内容的时候很少。求救!本人菜鸟无以为报,60分敬上! tcp 多线程 java 文件上传
[解决办法]
int len = 0;
while((len = in.read(buf)) != -1){
fOut.write(buf, 0, len);
}

fOut.flush();
fOut.close();

[解决办法]
flush
public void flush()
throws IOException刷新此输出流并强制写出所有缓冲的输出字节。flush 的常规协定是:如果此输出流的实现已经缓冲了以前写入的任何字节,则调用此方法指示应将这些字节立即写入它们预期的目标。
如果此流的预期目标是由基础操作系统提供的一个抽象(如一个文件),则刷新此流只能保证将以前写入到流的字节传递给操作系统进行写入,但不保证能将这些字节实际写入到物理设备(如磁盘驱动器)。

OutputStream 的 flush 方法不执行任何操作。


指定者:
接口 Flushable 中的 flush
抛出:
IOException - 如果发生 I/O 错误。

读书人网 >J2SE开发

热点推荐