2013年11月4日星期一

JAVA programming problem help ( network -peer file transfer program changes )

I knock on a network in a peer to peer file transfer and remote execution of the code found : the book 's source code is to be run separately on the two computers to transfer files , but I am currently on a computer therefore want to try to change the code to implement your own file transfer themselves receive files , so I use the code in the source file to be transmitted in the same directory, create a text document with a different name , want to receive their computer out the documents , and different input stream to write the name of the new text document , and automatically open the file in Notepad . The results showed that there is a successful file transfer tips, but in the new file does not open automatically, and manually open and found the received input stream does not successfully written , as well as throw an exception.
Here is a screenshot of my operating results , as well as the modified code , please help me look at what causes ? Trouble you !


import java.lang. *;
import java.awt. *;
import java.awt.event. *;
import java.net. *;
import java.io. *;
import javax.swing. *;

public class j03130401 extends Frame implements Runnable {
File recFileDir; / ​​/ To receive the documents contained in the parent directory
File F1 = new File ("testDir \ \ readme2.txt");
String reFile; / / To receive files
boolean canRecFile; / / whether you can receive files
/ / ===========================
String sendFile; / / I want to send a file

boolean canSendFile; / / if the file can be sent
/ / ============================
Label L1, L2, L3, L4, L5;
TextField T1, T2, T3, T4;
List list1;
Button B1;

public j03130401 () {
this.setLayout (null);
this.setBounds (200, 200, 370, 400);
this.setTitle ("TCP peer file transfer " ) ;
this.addWindowListener (new WindowAdapter () {
public void windowClosing (WindowEvent e) {
System.exit (0);
}
});
/ / ==================================== ==========
L1 = new Label (" the other IP:");
L1.setBounds (10, 25, 55, 25);
L1.setBackground (new Color (255,193,255));
this.add (L1);

T1 = new TextField ("192.168.1.100");
T1.setBounds (70,25,110,25);
this.add (T1);

L2 = new Label (" each received PORT:");
L2.setBounds (10, 60,120, 25);
L2.setBackground (new Color (255,193,255));
this.add (L2);

T2 = new TextField ("2222");
T2.setBounds (135, 60, 55, 25);
this.add (T2);

L3 = new Label (" I want to send the file : " ) ;
L3.setBounds (10,95,95,25);
L3.setBackground (new Color (255,193,255));
this.add (L3);

T3 = new TextField ("testDir \ \ readme.txt");
T3.setBounds (105,95,200,25);
this.add (T3);

B1 = new Button (" Send " ) ;
B1.setBounds (310, 95, 50, 25);
B1.addActionListener (new myActionListener ());
this.add (B1);

L4 = new Label (" Local received PORT:");
L4.setBounds (10, 150, 120, 25);
L4.setBackground (new Color (255,193,255));
this.add (L4);

T4 = new TextField ("2222");
T4.setBounds (135, 150, 55, 25);
T4.setEnabled (false);
this.add (T4);

L5 = new Label (" Receive file storage to: " ) ;
L5.setBounds (10, 185, 120, 25);
L5.setBackground (new Color (255,193,255));
this.add (L5);

list1 = new List ();
list1.setBounds (10, 220, 350, 160);
this.add (list1);
/ / ==================================== ==
this.setBackground (new Color (220,255,255));
this.setVisible (true);
} / / public j03130401 () end

public static void main (String [] args) {
File F1 = new File ("testDir \ \ readme2.txt");
try {
F1.createNewFile ();
}
catch (Exception e) {
JOptionPane.showMessageDialog (null, " Error 1 " ) ;
}


new Thread (new j03130401 ()). start ();
}

private boolean reFileName (ServerSocket serverTcp) throws IOException {
Socket connSocket = serverTcp.accept (); / / accepted client request to establish a connection machine
InputStreamReader serverInput = new InputStreamReader (connSocket.getInputStream ()); / / the byte stream flow into char
int data;
StringBuffer sb = new StringBuffer ();
while ((data = serverInput.read ())! = -1) {
sb.append (String.valueOf ((char) data)); / / verbatim read files stored on sb
}
reFile = sb.toString ();
serverInput.close ();
connSocket.close ();

if (reFile == "" | | reFile == null) {
return false;
}
else
return true;

}

private void checkFile () {
recFileDir = new File (reFile). getParentFile ();
if ((recFileDir! = null) && (! recFileDir.exists ())) {/ / there is the parent directory , and also exist
canRecFile = recFileDir.mkdirs ();
}
else
canRecFile = true;

}

public void reFile (ServerSocket serverTcp, byte [] theData) throws IOException {
if (! (this.reFileName (serverTcp)))
return;

checkFile ();
if (! canRecFile) { / / to receive the file directory is created.
JOptionPane.showMessageDialog (null, " Could not create directory " ) ;
return;
}
/ / =================================
Socket connSocket = serverTcp.accept (); / / accepts client requests to create an online
BufferedInputStream serverInput = new BufferedInputStream (connSocket.getInputStream ());
BufferedOutputStream FileObj = new BufferedOutputStream (new FileOutputStream (reFile));

while (serverInput.read (theData)! = -1) {
FileObj.write (theData); / / write to the local file
}
serverInput.close ();
FileObj.flush ();
FileObj.close ();
connSocket.close ();

list1.add (new File (reFile). getCanonicalPath ());
Runtime.getRuntime (). exec ("notepad" + reFile); / / try to open in Notepad just copy the files
boolean canRecFile = false;
String reFile = "";
File recFileDir = null;

}

public void run () {
byte [] theData = new byte [1];
try {
ServerSocket serverTcp = new ServerSocket (Integer.parseInt (T4.getText (). trim ()));
while (true) {
this.reFile (serverTcp, theData);
Thread.sleep (200);
}
}
catch (Exception e) {
JOptionPane.showMessageDialog (null, e.getMessage ());
}
} / / void run end

class myActionListener implements ActionListener {
public void sendFileName () throws UnknownHostException, IOException {
sendFile = T3.getText (). trim ();
reFile = F1.getName (). trim ();
File sourFile = new File (sendFile);
if (! sourFile.exists ()) {
canSendFile = false;
JOptionPane.showMessageDialog (null, " I want to send the file does not exist ! " ) ;
return;
}

Socket tcpSocket = new Socket (T1.getText (). trim (), Integer.parseInt (T2.getText (). trim ()));
BufferedOutputStream opStream = new BufferedOutputStream (tcpSocket.getOutputStream ());
opStream.write (reFile.getBytes (), 0, reFile.getBytes (). length);

opStream.flush (); / / clear the buffer data
opStream.close ();
tcpSocket.close ();
canSendFile = true;
}


public void sendFile () throws UnknownHostException, FileNotFoundException, IOException {
sendFileName ();
if (! canSendFile)
return;
/ / ======================
byte [] tmpData = new byte [1];
Socket tcpSocket = new Socket (T1.getText (). trim (), Integer.parseInt (T2.getText (). trim ()));
BufferedInputStream fileObj = new BufferedInputStream (new FileInputStream (sendFile));
BufferedOutputStream opStream = new BufferedOutputStream (tcpSocket.getOutputStream ());
while (fileObj.read (tmpData)! = -1) {
opStream.write (tmpData);
}

fileObj.close ();
opStream.flush ();
opStream.close ();
tcpSocket.close ();

canSendFile = false;
sendFile = "";
JOptionPane.showMessageDialog (null, " File transfer is completed ! " ) ;
}
public void actionPerformed (ActionEvent e) {
try {
this.sendFile ();
}
catch (IOException ecp) {
JOptionPane.showMessageDialog (null, " This exception does not appear " ) ;
}
}
}


}


------ Solution ------------------------------------ --------
reFile wrong path , only the file name, so that the implementation of the document should readme2.txt directory, not testDir next
------ For reference only - --------------------------------------
really nobody to help answer , no satisfactory answer Results posted was not any point , our only change number Results posted !
------ For reference only -------------------------------------- -
know what is debug, what is the single-step tracking you ?
------ For reference only -------------------------------------- -
Runtime.getRuntime (). exec ( "notepad" + reFile);

notepad price after space
------ For reference only ----------------------------- ----------

points to you, have abandoned java, switched to the web front-end development , java self really difficult , mainly errors do not know how to solve .

没有评论:

发表评论