📄 file_gui.java
字号:
if (array_size > 19)
{
data = new Object[array_size][3];
}
else
{
data = new Object[19][3]; // Minimum size of array...
}
boolean file_or_Folderectory;
int i = 1;
int g = 0;
while(i<array_size)
{
try
{
String temp = (String)values.get(i);
file_name = temp.substring(temp.lastIndexOf("\\")+1);
if (file_name.equals(""))
{
information[g][0] = (String)values.get(i);
data[g][0] = values.get(i);
information[g][2] = "Folder";
data[g][2] = "Folder";
i++;
information[g][3] = (String)values.get(i);
i++;
data[g][1] = (Object)" - ";
}
else
{
information[g][0] = (String)values.get(i);
int index =file_name.lastIndexOf(".");
String gh = "";
if (index == -1)
{
information[g][2] = "";
data[g][2] = information[g][2]+" File";
gh = file_name;
}
else
{
information[g][2] = file_name.substring(file_name.lastIndexOf(".")+1);
data[g][2] = information[g][2]+" File";
gh = file_name.substring(0,index);
}
data[g][0] = gh;
information[g][2] = file_name.substring(file_name.lastIndexOf(".")+1);
data[g][2] = information[g][2]+" File";
i++;
information[g][3] = (String)values.get(i);
i++;
information[g][1] = (String)values.get(i);
data[g][1] = values.get(i);
i++;
}
}
catch (Exception e)
{
System.out.println( e );
}
g++;
file_name = "";
} // End While.....
} // end if..
else
{
information = new String[1][3];
information[0][0] = "No Files are shareable";
information[0][1] = " ";
information[0][2] = " ";
data[0][0] = (Object)"No Files are shareable";
data[0][1] =(Object)" - ";
data[0][2] =(Object)" - ";
}
}
/* This class is used to download the file from the listener on to the Client's
machine....
*/
public class download_file extends JFrame
{
download_file() // Constructor...
{
try
{
int row = 0;
row = file_listing.getSelectedRow(); // Get the selection of the user...
InetAddress inet = InetAddress.getByName(ip_address);
file_socket = new Socket(inet,7070); // Establish a socket connection with
// the Listener on the port 7070
// address -- inet..
// Get The output as well as the input Streams on that socket...
BufferedOutputStream out = new BufferedOutputStream(file_socket.getOutputStream());
BufferedInputStream br_socket = new BufferedInputStream(file_socket.getInputStream());
// if the selection is a folder... then pop up a message for denial of
// download..
if (information[row][2].equalsIgnoreCase("Folder"))
{
JOptionPane.showMessageDialog(this,"Cannot Download a Folder. Try Opening it. ","Peer 2 Peer...",JOptionPane.INFORMATION_MESSAGE);
}
else // If the request is that of a file..
{
XmlWriter writer = new XmlWriter(); // Call a class XMLWRITER to generate
// request by using a function
writer.requestFString("DOWNLOAD",information[row][0]); // requestFString...
String file_data = writer.returnRequest();
byte file_bytes[] = file_data.getBytes(); // get the Number of bytes from the
// request...
String temporary =information[row][0].substring(information[row][0].lastIndexOf("\\")+1);
JFileChooser jfc = new JFileChooser(); // Call an object of JFileChooser
// File Dialog to place thefile.
File file = new File (temporary);
jfc.setSelectedFile(file);
jfc.ensureFileIsVisible(file);
int button_pressed = jfc.showSaveDialog(this);
String str1 = "";
File file_final = jfc.getSelectedFile();
if (button_pressed == JFileChooser.APPROVE_OPTION)
{
str1 = file_final.getPath();// Get the path where the file is being saved..
}
BufferedOutputStream out_file = new BufferedOutputStream(new FileOutputStream(str1)); // Create an outputstream to that path...
int file_size = file_bytes.length;
// Adjust the request length to 1024
// (for c#) listener's...
byte b[] = new byte[1024];
// another method of the class add_on is used now (apporpriatelength) this
// is used so as to make the request sent by the client to the listener
// 1024 in length..(for c#) listener's...
// The methos takes a byte array and it's length as parameters and return
// a byte array of length 1024 bytes....
add_on download = new add_on();
b = download.appropriatelength(file_bytes, file_size);
out.write(b,0,1024); // The byte array is written on the output stream
int y = 0;
byte f[] = new byte[32];
int file1_size = Integer.parseInt(information[row][1]);
// Generate a progress monitor to monitor the request...
ProgressMonitor pm = new ProgressMonitor(this,"Downloading File..","Downloading Please Wait...",0,file1_size);
int current = 0;
pm.setMillisToPopup(5);
while ((y = br_socket.read(f,0,32))>0) // Read the socket and write on to the
// file...
{
out_file.write(f,0,y);
current = current + y;
pm.setProgress(current); // Monitoring the progress of the progress
// monitor.,..
}
out.close(); //
br_socket.close(); // The filestream and socket streams are
out_file.close(); // Closed
}
}
catch(Exception es)
{
}
try
{
file_socket.close(); // Close the Client_socket...
}
catch (Exception e)
{
System.out.println( "Some Error while Closing : "+e );
}
} // End Constructor..
} // End Class Download_file...
/* This class is used to upload the file from the Client on to the Listener's
machine....
*/
public class upload_file extends JFrame
{
upload_file() // Constructor...
{
String str1 ="";
Vector parameters = new Vector();
try
{
InetAddress inet = InetAddress.getByName(ip_address);
file_socket = new Socket(inet,7070);// Establish a socket connection with
// the Listener on the port 7070
// address -- inet..
// Get The output as well as the input Streams on that socket...
BufferedOutputStream out = new BufferedOutputStream(file_socket.getOutputStream());
BufferedInputStream br_socket = new BufferedInputStream(file_socket.getInputStream());
// if the upload is in a root... then pop up a message for denial of
// download..
// if the flag is a 0... then pop up a message for denial of
// download..
if (status_text.equalsIgnoreCase("ROOT"))
{
JOptionPane.showMessageDialog(this,"Cannot Upload In Root. Try sub Folders . ","Peer 2 Peer...",JOptionPane.INFORMATION_MESSAGE);
}
else if (flag_info.equals("0"))
{
JOptionPane.showMessageDialog(this,"Cannot Upload In Read Only Folder . Try other Folders . ","Peer 2 Peer...",JOptionPane.INFORMATION_MESSAGE);
}
else // Else if flag = 1
{
JFileChooser jfc = new JFileChooser(); // Call an object of JFileChooser
// File Dialog to choose the file.
int button_pressed = jfc.showOpenDialog(this);
File file_final = jfc.getSelectedFile();
if (button_pressed == JFileChooser.APPROVE_OPTION)
{
str1 = file_final.getPath(); // Get the path of the file
}
String temp = str1.substring(str1.lastIndexOf("\\")+1);
temp = status_text+temp;
XmlWriter writer = new XmlWriter(); // Call a class XMLWRITER to generate
// request by using a function
writer.requestFString("UPLOAD",temp); // requestFString...
String file_data = writer.returnRequest();
byte file_bytes[] = file_data.getBytes(); // get the bytes from the
// request...
BufferedInputStream file_read = new BufferedInputStream(new FileInputStream(str1)); // Create an inputstream to that path...
int upload_file_size = file_read.available();
int file_size = file_bytes.length;
byte b[] = new byte[1024];
// Adjust the request length to 1024
// (for c#) listener's...
// another method of the class add_on is used now (apporpriatelength) this
// is used so as to make the request sent by the client to the listener
// 1024 in length..(for c#) listener's...
// The methos takes a byte array and it's length as parameters and return
// a byte array of length 1024 bytes....
add_on upload = new add_on();
b = upload.appropriatelength(file_bytes, file_size);
out.write(b,0,1024); // The byte array is written on the output stream
int y = 0;
byte f[] = new byte[1024];
// Generate a progress monitor to monitor the request...
ProgressMonitor pm = new ProgressMonitor(this,"Uploading File..","Uploading Please Wait...",0,upload_file_size);
int current = 0;
//pm.setMillisToPopup(5);
while ((y = file_read.read(f,0,1024))>0)// Read the file and write on socket
{
out.write(f,0,y);
current = current + y ;
pm.setProgress(current); // Monitor the current activity...
}
out.close();
br_socket.close(); // Close the streams...
}
}
catch(Exception es)
{}
try
{
file_socket.close(); // Close the socket...
}
catch (Exception e)
{
System.out.println( "Some Error while Closing : "+e );
}
} // End Constructor....
} // END UPLOAD FILE...
} // End File_Gui...
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -