想通过Socket传送文件,但是总是提示接收的流读的是null
代码如下:
import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.PrivateKey;
import java.security.PublicKey;import javax.swing.JButton;import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;public class User extends JFrame{ /**
* @param args
*/
private Socket conn;
private JTextField jt_Ip;
private JLabel jLabel2;
private JButton jb_send;
private JTextField jt_port;
private JLabel jLabel1;
private SendFileWithSign sfs;
private RecieveFileWithSign rfs;
private ServerSocket waits;
private PublicKey pubk;
private PrivateKey prik;
private File f;
private JTextField jt_id;
private JLabel jLabel3;
private JFileChooser jfc;
private DataInputStream is;
private DataOutputStream os;
private DataInputStream sis;
private DataOutputStream sos; public User(){
this.initGUI();
this.setVisible(true);
try {
waits=new ServerSocket(10009);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} new Thread(new wait()).start();
}
private void initGUI() {
try {
AnchorLayout thisLayout = new AnchorLayout();
getContentPane().setLayout(thisLayout);
{
jt_id = new JTextField();
getContentPane().add(jt_id, new AnchorConstraint(468, 817, 566, 260, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
jt_id.setPreferredSize(new java.awt.Dimension(237, 22));
jt_id.setText("adfad");
}
{
jLabel3 = new JLabel();
getContentPane().add(jLabel3, new AnchorConstraint(482, 217, 548, 43, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
jLabel3.setText("\u81ea\u5df1\u7684id");
jLabel3.setPreferredSize(new java.awt.Dimension(74, 15));
}
{
jb_send = new JButton();
getContentPane().add(jb_send, new AnchorConstraint(664, 325, 762, 43, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
jb_send.setText("\u53d1\u9001\u6587\u4ef6");
jb_send.setPreferredSize(new java.awt.Dimension(120, 22));
jb_send.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try { jfc=new JFileChooser();
jfc.setDialogTitle("请选择自己的私钥文件");
jfc.showOpenDialog(null);
f=jfc.getSelectedFile();
prik=(PrivateKey) Tools.readKey(f);
jfc.setDialogTitle("请选择要发送的文件");
jfc.showOpenDialog(null);
f=jfc.getSelectedFile();
byte[] bt=Tools.s2b(jt_id.getText()+":"+f.getName());
conn=new Socket(jt_Ip.getText(),Integer.parseInt(jt_port.getText()));
if(conn.isConnected()){
os=new DataOutputStream(conn.getOutputStream());
is=new DataInputStream(conn.getInputStream());
os.write(bt, 0, bt.length);
os.flush();
System.out.println("send requery."+bt.length);
System.out.println(Tools.b2s(bt));
new Thread(new send()).start();
}
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}
{
jt_port = new JTextField();
getContentPane().add(jt_port, new AnchorConstraint(313, 817, 411, 257, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
jt_port.setText("10009");
jt_port.setPreferredSize(new java.awt.Dimension(238, 22));
}
{
jLabel2 = new JLabel();
getContentPane().add(jLabel2, new AnchorConstraint(326, 180, 393, 43, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
jLabel2.setText("\u5bf9\u65b9\u7aef\u53e3");
jLabel2.setPreferredSize(new java.awt.Dimension(58, 15));
}
{
jLabel1 = new JLabel();
getContentPane().add(jLabel1, new AnchorConstraint(144, 257, 211, 43, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
jLabel1.setText("\u5bf9\u65b9IP");
jLabel1.setPreferredSize(new java.awt.Dimension(91, 15));
}
{
jt_Ip = new JTextField();
getContentPane().add(jt_Ip, new AnchorConstraint(126, 817, 233, 257, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
jt_Ip.setText("127.0.0.1");
jt_Ip.setPreferredSize(new java.awt.Dimension(238, 24));
}
{
this.setSize(433, 259);
}
} catch(Exception e) {
e.printStackTrace();
}
} private class wait implements Runnable{
String usrName;
String s_temp,s_file;
int p;
Socket sk;
byte[] temp;
@Override
public void run() {
while(true){
try {
sk = waits.accept();
sis=new DataInputStream(sk.getInputStream());
sos=new DataOutputStream(sk.getOutputStream());
System.out.println(sis.toString());
sis.read(temp);
s_temp=Tools.b2s(temp);
p= s_temp.indexOf(":");
if(p>0)
{
s_temp=Tools.b2s(temp);
usrName=s_temp.substring(0,p);
s_file=s_temp.substring(p+1);
jfc=new JFileChooser();
jfc.setDialogTitle(usrName+"要给你发送文件:"+s_file+",请选择他的公钥");
jfc.showSaveDialog(null);
f=jfc.getSelectedFile();
pubk=(PublicKey) Tools.readKey(f);
jfc.setDialogTitle(usrName+"要给你发送文件:"+s_file+",请保存");
jfc.showSaveDialog(null);
f=jfc.getSelectedFile();
rfs=new RecieveFileWithSign(pubk, f, sk);
sos.write(Tools.s2b("ok"),0,Tools.s2b("ok").length);
System.out.println("ok");
}
else{
rfs.recive();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private class send implements Runnable{
byte[] b_temp;
@Override
public void run() {
while(conn.isConnected()){
try {
// is=new DataInputStream(conn.getInputStream());
is.read(b_temp);
if(Tools.b2s(b_temp)=="ok")
{
sfs=new SendFileWithSign(prik, f, conn);
sfs.send();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
代码如下:
import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.PrivateKey;
import java.security.PublicKey;import javax.swing.JButton;import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;public class User extends JFrame{ /**
* @param args
*/
private Socket conn;
private JTextField jt_Ip;
private JLabel jLabel2;
private JButton jb_send;
private JTextField jt_port;
private JLabel jLabel1;
private SendFileWithSign sfs;
private RecieveFileWithSign rfs;
private ServerSocket waits;
private PublicKey pubk;
private PrivateKey prik;
private File f;
private JTextField jt_id;
private JLabel jLabel3;
private JFileChooser jfc;
private DataInputStream is;
private DataOutputStream os;
private DataInputStream sis;
private DataOutputStream sos; public User(){
this.initGUI();
this.setVisible(true);
try {
waits=new ServerSocket(10009);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} new Thread(new wait()).start();
}
private void initGUI() {
try {
AnchorLayout thisLayout = new AnchorLayout();
getContentPane().setLayout(thisLayout);
{
jt_id = new JTextField();
getContentPane().add(jt_id, new AnchorConstraint(468, 817, 566, 260, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
jt_id.setPreferredSize(new java.awt.Dimension(237, 22));
jt_id.setText("adfad");
}
{
jLabel3 = new JLabel();
getContentPane().add(jLabel3, new AnchorConstraint(482, 217, 548, 43, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
jLabel3.setText("\u81ea\u5df1\u7684id");
jLabel3.setPreferredSize(new java.awt.Dimension(74, 15));
}
{
jb_send = new JButton();
getContentPane().add(jb_send, new AnchorConstraint(664, 325, 762, 43, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
jb_send.setText("\u53d1\u9001\u6587\u4ef6");
jb_send.setPreferredSize(new java.awt.Dimension(120, 22));
jb_send.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try { jfc=new JFileChooser();
jfc.setDialogTitle("请选择自己的私钥文件");
jfc.showOpenDialog(null);
f=jfc.getSelectedFile();
prik=(PrivateKey) Tools.readKey(f);
jfc.setDialogTitle("请选择要发送的文件");
jfc.showOpenDialog(null);
f=jfc.getSelectedFile();
byte[] bt=Tools.s2b(jt_id.getText()+":"+f.getName());
conn=new Socket(jt_Ip.getText(),Integer.parseInt(jt_port.getText()));
if(conn.isConnected()){
os=new DataOutputStream(conn.getOutputStream());
is=new DataInputStream(conn.getInputStream());
os.write(bt, 0, bt.length);
os.flush();
System.out.println("send requery."+bt.length);
System.out.println(Tools.b2s(bt));
new Thread(new send()).start();
}
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}
{
jt_port = new JTextField();
getContentPane().add(jt_port, new AnchorConstraint(313, 817, 411, 257, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
jt_port.setText("10009");
jt_port.setPreferredSize(new java.awt.Dimension(238, 22));
}
{
jLabel2 = new JLabel();
getContentPane().add(jLabel2, new AnchorConstraint(326, 180, 393, 43, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
jLabel2.setText("\u5bf9\u65b9\u7aef\u53e3");
jLabel2.setPreferredSize(new java.awt.Dimension(58, 15));
}
{
jLabel1 = new JLabel();
getContentPane().add(jLabel1, new AnchorConstraint(144, 257, 211, 43, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
jLabel1.setText("\u5bf9\u65b9IP");
jLabel1.setPreferredSize(new java.awt.Dimension(91, 15));
}
{
jt_Ip = new JTextField();
getContentPane().add(jt_Ip, new AnchorConstraint(126, 817, 233, 257, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
jt_Ip.setText("127.0.0.1");
jt_Ip.setPreferredSize(new java.awt.Dimension(238, 24));
}
{
this.setSize(433, 259);
}
} catch(Exception e) {
e.printStackTrace();
}
} private class wait implements Runnable{
String usrName;
String s_temp,s_file;
int p;
Socket sk;
byte[] temp;
@Override
public void run() {
while(true){
try {
sk = waits.accept();
sis=new DataInputStream(sk.getInputStream());
sos=new DataOutputStream(sk.getOutputStream());
System.out.println(sis.toString());
sis.read(temp);
s_temp=Tools.b2s(temp);
p= s_temp.indexOf(":");
if(p>0)
{
s_temp=Tools.b2s(temp);
usrName=s_temp.substring(0,p);
s_file=s_temp.substring(p+1);
jfc=new JFileChooser();
jfc.setDialogTitle(usrName+"要给你发送文件:"+s_file+",请选择他的公钥");
jfc.showSaveDialog(null);
f=jfc.getSelectedFile();
pubk=(PublicKey) Tools.readKey(f);
jfc.setDialogTitle(usrName+"要给你发送文件:"+s_file+",请保存");
jfc.showSaveDialog(null);
f=jfc.getSelectedFile();
rfs=new RecieveFileWithSign(pubk, f, sk);
sos.write(Tools.s2b("ok"),0,Tools.s2b("ok").length);
System.out.println("ok");
}
else{
rfs.recive();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private class send implements Runnable{
byte[] b_temp;
@Override
public void run() {
while(conn.isConnected()){
try {
// is=new DataInputStream(conn.getInputStream());
is.read(b_temp);
if(Tools.b2s(b_temp)=="ok")
{
sfs=new SendFileWithSign(prik, f, conn);
sfs.send();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
解决方案 »
- 出了几个面试题,用来面试毕业生,大家看看难度是否合适?
- 以后想在程序这方面发展,现在大三,要考什么证吗?考什么证好?
- SunYi 还是有点小Bug,Tab页不能全部关闭,会抛异常。帮忙看看。谢谢
- innerHTML和frame结合请求?
- 异常寻找解决办法
- JTree如何将新添加的叶子节点变成目录而非文件的形式?
- 半角符号
- jni是怎么回事,能用jni读取windows的dll文件吗
- 单独的类之间的,参数传递问题,急呀,在线等,跪求高手指点.......
- 能不能在一个frame里动态的添加label啊???
- 急求答案
- arraylist<class>客户端服务器传递时ClassNotFound的问题
import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class testsocket {
ServerSocket ss;
Socket sk;
DataInputStream dis;
DataOutputStream dos;
byte[] buff;
/**
* @param args
*/
public static void main(String[] args) {
testsocket a=new testsocket();
new Thread(a.new wait()).start();
new Thread(a.new send()).start();
}
class wait implements Runnable{ @Override
public void run() {
try {
ss=new ServerSocket(12345);
sk=ss.accept();
while(sk.isConnected()){
dis=new DataInputStream(sk.getInputStream());
dis.read(buff);
System.out.println(Tools.b2s(buff));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class send implements Runnable{
String s;
Scanner in;
byte[] in_temp;
@Override
public void run() {
try {
sk=new Socket("127.0.0.1",12345);
s="";
while (!s.startsWith("exit")){
dos=new DataOutputStream(sk.getOutputStream());
in=new Scanner(System.in);
s=in.nextLine();
in_temp=Tools.s2b(s);
System.out.println(s);
dos.write(in_temp, 0, in_temp.length);
dos.flush();
}
System.exit(0);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}}
public static String b2s(byte[] b){
char[] t;
int i=0;
t=new char[b.length];
while(i<b.length){
t[i]=(char)b[i];
i++;
}
return new String(t);
} public static byte[] s2b(String b){
byte[] t;
int i=0;
t=new byte[b.length()];
while(i<b.length()){
t[i]=(byte)b.charAt(i);
i++;
}
return t;
}
public static String b2s(byte[] b){
char[] t;
int i=0;
t=new char[b.length];
while(i<b.length){
t[i]=(char)b[i];
i++;
}
return new String(t);
} public static byte[] s2b(String b){
byte[] t;
int i=0;
t=new byte[b.length()];
while(i<b.length()){
t[i]=(byte)b.charAt(i);
i++;
}
return t;
}
}//testsocket.java//**************************************
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class testsocket {
ServerSocket ss;
Socket sk;
DataInputStream dis;
DataOutputStream dos;
byte[] buff;
/**
* @param args
*/
public static void main(String[] args) {
testsocket a=new testsocket();
new Thread(a.new wait()).start();
new Thread(a.new send()).start();
}
class wait implements Runnable{ @Override
public void run() {
try {
ss=new ServerSocket(12345);
sk=ss.accept();
while(sk.isConnected()){
dis=new DataInputStream(sk.getInputStream());
dis.read(buff);
System.out.println(Tools.b2s(buff));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class send implements Runnable{
String s;
Scanner in;
byte[] in_temp;
@Override
public void run() {
try {
sk=new Socket("127.0.0.1",12345);
s="";
while (!s.startsWith("exit")){
dos=new DataOutputStream(sk.getOutputStream());
in=new Scanner(System.in);
s=in.nextLine();
in_temp=Tools.s2b(s);
System.out.println(s);
dos.write(in_temp, 0, in_temp.length);
dos.flush();
}
System.exit(0);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}}
public class Tools {
public static String b2s(byte[] b){
char[] t;
int i=0;
t=new char[b.length];
while(i<b.length){
t[i]=(char)b[i];
i++;
}
return new String(t);
}public static byte[] s2b(String b){
byte[] t;
int i=0;
t=new byte[b.length()];
while(i<b.length()){
t[i]=(byte)b.charAt(i);
i++;
}
return t;
}
}testsocket.javaimport java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class testsocket {
ServerSocket ss;
Socket sk;
DataInputStream dis;
DataOutputStream dos;
byte[] buff;
/**
* @param args
*/
public static void main(String[] args) {
testsocket a=new testsocket();
new Thread(a.new wait()).start();
new Thread(a.new send()).start();
}
class wait implements Runnable{@Override
public void run() {
try {
ss=new ServerSocket(12345);
sk=ss.accept();
while(sk.isConnected()){
dis=new DataInputStream(sk.getInputStream());
dis.read(buff);
System.out.println(Tools.b2s(buff));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}class send implements Runnable{
String s;
Scanner in;
byte[] in_temp;
@Override
public void run() {
try {
sk=new Socket("127.0.0.1",12345);
s="";
while (!s.startsWith("exit")){
dos=new DataOutputStream(sk.getOutputStream());
in=new Scanner(System.in);
s=in.nextLine();
in_temp=Tools.s2b(s);
System.out.println(s);
dos.write(in_temp, 0, in_temp.length);
dos.flush();
}
System.exit(0);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}}