黑马程序员技术交流社区
标题:
swing上传文件,求大神运行,修改下, 这里不知道怎么错了
[打印本页]
作者:
ノtrack
时间:
2014-5-8 12:33
标题:
swing上传文件,求大神运行,修改下, 这里不知道怎么错了
import java.net.ServerSocket;
import java.net.Socket;
/**
* 9、 使用TCP协议写一个可以上传文件的服务器和客户端。
*
* @author 刘阳文
* */
public class Test9
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(8888);
while(true)
{
Socket socket = ss.accept();
new Thread(new TcpServer(socket)).start();
}
}
}
复制代码
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
/**
* Tcp文件上传 服务端
*
* @author 刘阳文
* */
public class TcpServer implements Runnable
{
private Socket socket;
public TcpServer(Socket socket)
{
this.socket = socket;
}
public void run()
{
OutputStream outputStream = null;
try
{
InputStream inputStream = socket.getInputStream();
BufferedInputStream bis = new BufferedInputStream(inputStream);
bis.mark(0);
byte[] buf = new byte[1024];
int len = 0;
len = bis.read(buf);
String str = new String(buf,0,len);
String[] strs = str.split("----");
bis.reset();
bis.skip((strs[0].length()+"----".length())+(int)8);
String[] fileNames = strs[0].substring(strs[0].lastIndexOf(File.separatorChar)+1).split("\\.");
int count = 0;
String fileName = fileNames[0]+"("+(++count)+")."+fileNames[1];
File file = new File(fileName);
if(file.exists())
file = new File(fileName);
outputStream = new FileOutputStream(file);
while((len = bis.read(buf)) > 0)
{
outputStream.write(buf, 0, len);
}
} catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if(outputStream != null)
outputStream.close();
} catch (Exception e2)
{
System.out.println("文件写入流关闭异常");
}
try
{
if(socket != null)
socket.close();
} catch (Exception e2)
{
System.out.println("socket关闭异常");
}
}
}
}
复制代码
import java.awt.Button;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
/**
* Tcp文件上传 客户端
* */
@SuppressWarnings("serial")
public class TcpClient extends Frame
{
private TextField ipField ;
private TextField porField;
private Label iplLabel;
private Label portlLabel;
private TextField filePath ;
private Label fileLabel;
private FileDialog openFile;
private Button openButton;
private TextArea info;
private Button send;
private Socket socket;
private InetSocketAddress isa;
public TcpClient (Socket socket)
{
super("客户端 文件上传");
this.socket = socket;
init();
}
private void init()
{
setBounds(300, 300, 500, 200);
setLayout(new FlowLayout());
ipField = new TextField(30);
ipField.setText("127.0.0.1");
iplLabel = new Label("服务端IP地址:");
porField = new TextField(5);
porField.setText("8888");
portlLabel = new Label("端口号:");
fileLabel = new Label("文件路径:");
filePath = new TextField(35);
openButton = new Button("选择文件");
send = new Button("上传文件");
info = new TextArea(5,65);
add(iplLabel);
add(ipField);
add(portlLabel);
add(porField);
add(fileLabel);
add(filePath);
add(openButton);
add(send);
add(info);
actionEvent();
windowEvent();
keyEvent();
setVisible(true);
}
public static void main(String[] args)
{
Socket socket = new Socket();
new TcpClient(socket);
}
private void actionEvent()
{
openButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
openFile = new FileDialog(TcpClient.this,"选择上传的文件",FileDialog.LOAD);
openFile.setVisible(true);
String path = openFile.getDirectory();
String file = openFile.getFile();
if(path != null && file != null){
filePath.setText(path+file);
}
}
});
send.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
int port=0;
InputStream inputStream = null;
try
{
System.out.println("上传文件"+filePath.getText());
port = Integer.parseInt(porField.getText());
isa =new InetSocketAddress(ipField.getText(),port);
if(!socket.isConnected()){
socket.connect(isa);
inputStream = new FileInputStream(new File(filePath.getText()));
OutputStream outputStream = socket.getOutputStream();
outputStream.write((filePath.getText()+"----").getBytes());
byte[] buf = new byte[1024];
int len = 0;
while((len = inputStream.read(buf)) > 0)
{
outputStream.write(buf, 0, len);
}
System.out.println("上传成");
}
filePath.setText("");
} catch (Exception e2)
{
e2.printStackTrace();
}
finally
{
try
{
if(inputStream != null )
inputStream.close();
} catch (Exception e3)
{
System.out.println("文件读取流关闭异常");
}
}
}
});
}
private void windowEvent()
{
//关闭窗口
this.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
});
}
private void keyEvent()
{
ipField.addKeyListener(new KeyAdapter()
{
// 35065 35071 35073
@Override
public void keyPressed(KeyEvent event)
{
//ip地址只能输入. 0-9 数字键盘区
if(!((event.getKeyCode()>=96 && event.getKeyCode()<=105) || event.getKeyCode()==110))
event.consume();
}
});
porField.addKeyListener(new KeyAdapter()
{
@Override
public void keyPressed(KeyEvent event)
{
//端口只能输入地址
if(!(event.getKeyCode()>=96 && event.getKeyCode()<=105))
event.consume();
}
});
}
}
复制代码
作者:
ノtrack
时间:
2014-5-8 12:34
帮忙运行 修改下 搞了半天
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2