求助:java socket读写文件问题!

yuanrong303 2008-08-04 02:07:05
代码主要想实现功能:
1、客户端读取文件夹所有文本文件,把这些文件一个一个发送到服务器端。
2、客户端实现一个Timer,定时查看文件夹有没有文件,有就建立socket连接,没有就不建立。
3、服务器端实现多线程接受文件。

客户端代码:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.*;

/**
* 本程序通过socket编程实现从客户端发送文件到服务器端
* @author ×××
*/
class FileTimer extends TimerTask{
public void run(){
byte[] buffer = new byte[1024];
int length = 0;
try {
Socket socket = new Socket("localhost", 5100);
File f = new File("d:\\text\\");
File[] tempFile=f.listFiles();
if(tempFile!=null){
for(int i=0;i<tempFile.length;i++){
FileInputStream fis = new FileInputStream(tempFile[i]);
DataInputStream dataIn = new DataInputStream(fis);
DataOutputStream dataOut = new DataOutputStream(socket
.getOutputStream());

while ((length = dataIn.read(buffer)) != -1) {
dataOut.write(buffer, 0, length);
}
dataOut.flush();
dataOut.close();
fis.close();
tempFile[i].delete();
System.out.println("文件传送完毕!!!");
socket.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class FileClient {
public static void main(String[] args) {
Timer a=new Timer();
a.schedule(new FileTimer(), 0, 60000);
}
}


服务器端代码:


import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;
import java.text.*;


/**
* 本程序通过socket编程实现从客户端发送文件到服务器端
* @author ×××
*/
/**
* 构造一个现在时间为基点的字符串作为文件输入文件的文件名
*/
class TimeStr{
//Date CurrentTime=new Date();
public static String getTimeStr() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
String dateString = formatter.format(currentTime);
String[] str=dateString.split(" ");
return "D:\\rec\\"+str[0]+str[1]+".txt";
}
}
/**
* 多线程类实现服务器一直都在监听
*/
class MultiServer extends Thread{
private Socket socket;
private DataInputStream dataIn;
private FileOutputStream out;
int length=0;
byte[] buffer=new byte[1024];
public MultiServer(Socket s)throws IOException{

socket=s;
dataIn= new DataInputStream(socket.getInputStream());

File f = new File(TimeStr.getTimeStr());
if (!f.exists())
f.createNewFile();
out = new FileOutputStream(f);
start();
}

public void run(){
try{
while((length=dataIn.read(buffer))!=-1){
out.write(buffer, 0, length);
}
}catch(IOException e){
e.printStackTrace();
}finally{
try{
out.flush();
out.close();
System.out.println("服务器端文件接收完毕!!!");
socket.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}

//
public class FileServer {
static final int PORT=5100;
public static void main(String[] a)throws IOException {
ServerSocket s = new ServerSocket(PORT);
System.out.println("服务器启动!");
try{
while(true){
Socket socket=s.accept();
try{
new MultiServer(socket);
}catch(IOException e){
socket.close();
e.printStackTrace();
}
}
}catch(IOException e){
e.printStackTrace();
}finally{
s.close();
}
}

}




现在只要问题:
1、如果文件夹有多个文件,Timer一次只传一个文件,另一个要一分钟后再转。
2、如果文件夹没有文夹 ,发现服务器端会创建一个空的文件,代码中有判断为什么还会出现这个问题。
...全文
511 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
mowobi 2009-03-31
  • 打赏
  • 举报
回复
小弟也是刚学,希望各位多多指教!
xtbzqw 2008-08-06
  • 打赏
  • 举报
回复
你肯定是这样啊,你客户端是1分钟启动一次任务所以就一分钟传输一次啊,我说点个人意见,你要是想传完就接着传
你用线程去实现啊,用一个线程专门监控传输啊,如果在一分钟类传完了,就开启一个传输任务!
至于你的第二个问题:
我感觉是你的这个代码造成的
File[] tempFile=f.listFiles();
if(tempFile!=null){
这是jdk的api上的说明:
public File[] listFiles()返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件。
如果此抽象路径名不表示一个目录,那么此方法将返回 null。否则返回一个 File 对象数组,每个数组元素对应目录中的每个文件或目录。表示目录本身及其父目录的名称不包括在结果中。得到的每个抽象路径名都是根据此抽象路径名,使用 File(File, String) 构造方法构造的。所以,如果此路径名是绝对路径名,那么得到的每个路径名都是绝对路径名;如果此路径名是相对路径名,那么得到的每个路径名都是相对于同一目录的路径名。

不保证所得数组中的相同字符串将以特定顺序出现,特别是不保证它们按字母顺序出现。


返回:
抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件和目录。如果目录为空,那么数组也将为空。如果抽象路径名不表示一个目录,或者发生 I/O 错误,则返回 null。

xuegao199 2008-08-06
  • 打赏
  • 举报
回复
第二个问题也是因为Socket创健的问题

你是在 if(tempFile!=null){
之前创建的,也就是说无论有没有文件要传,你都要创建一个链接,而你在服务器端,只要有链接就创建文件。
改为:

class FileTimer extends TimerTask{
public void run(){
byte[] buffer = new byte[1024];
int length = 0;
try {
File f = new File("d:\\text\\");
File[] tempFile=f.listFiles();
if(tempFile!=null&&tempFile.length>0){
Socket socket = new Socket("localhost", 5100);
for(int i=0;i <tempFile.length;i++){
FileInputStream fis = new FileInputStream(tempFile[i]);
DataInputStream dataIn = new DataInputStream(fis);
DataOutputStream dataOut = new DataOutputStream(socket
.getOutputStream());

while ((length = dataIn.read(buffer)) != -1) {
dataOut.write(buffer, 0, length);
}
dataOut.flush();
dataOut.close();
fis.close();
tempFile[i].delete();
System.out.println("文件传送完毕!!!");
}
socket.close();//Socket在for外半闭就可以了

}
} catch (IOException e) {
e.printStackTrace();
}
}
}
应该OK了

我没有试
兄弟试一下吧
xuegao199 2008-08-06
  • 打赏
  • 举报
回复
以上是第一个问题的解决方案
xuegao199 2008-08-06
  • 打赏
  • 举报
回复
第二个问题是因为你在传完一个文件后就把Socket关闭了

改为
class FileTimer extends TimerTask{
public void run(){
byte[] buffer = new byte[1024];
int length = 0;
try {
Socket socket = new Socket("localhost", 5100);
File f = new File("d:\\text\\");
File[] tempFile=f.listFiles();
if(tempFile!=null){
for(int i=0;i<tempFile.length;i++){
FileInputStream fis = new FileInputStream(tempFile[i]);
DataInputStream dataIn = new DataInputStream(fis);
DataOutputStream dataOut = new DataOutputStream(socket
.getOutputStream());

while ((length = dataIn.read(buffer)) != -1) {
dataOut.write(buffer, 0, length);
}
dataOut.flush();
dataOut.close();
fis.close();
tempFile[i].delete();
System.out.println("文件传送完毕!!!");
}
socket.close();//Socket在for外半闭就可以了

}
} catch (IOException e) {
e.printStackTrace();
}
}
}
yuanrong303 2008-08-04
  • 打赏
  • 举报
回复
请各位多指点,小弟刚学。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧