how to increase the file size to be upload or download between client and server
Hi everybody , I'd like u to help me to increase the file size that will upload or download between client and server " now i can send or receive 70 mb but once i want to increase the size to for instance 300 or 400 MB it doesn't accept
Here is part of the code
Server code :
Code:
import java.io.*;
import java.net.*;
import javax.swing.*;
public class ServerGUI extends javax.swing.JFrame {
public String path; //serverFile path
public String str = ""; //file shares list
public String remoteClientAddress = "";
public ServerSocket servSocket = null; //tcp socket for server
public Socket sock = null; //server socket for connection management
public int servPort = 0; //server Port number
public DataOutputStream fileDir = null; //to sent shares file name
public DataInputStream selectedFile = null; //to accept client selected file name
public DataInputStream command = null; //to sent or receive file
public int clientConnectStatus = 0; //0 = client connect for the first time
public int fileShareList = 0; //0 = sending file share list for the first time
public Thread readThread; //mainThread to handle connection for server
public File Filelist [];
/** Creates new form ServerGUI */
public ServerGUI() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel4 = new javax.swing.JLabel();
jScrollPane2 = new javax.swing.JScrollPane();
DirectoryInfo = new javax.swing.JTextArea();
jLabel5 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
StartServer = new javax.swing.JButton();
StopServer = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
PortSelected = new javax.swing.JTextField();
PortSelection = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
ActivityLog = new javax.swing.JTextArea();
SetPath = new javax.swing.JButton();
TF_FilePath = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Server interface ");
setBackground(new java.awt.Color(0, 255, 255));
setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
setForeground(java.awt.Color.white);
setResizable(false);
jLabel4.setText("* Port Number must be between(1024-65534)");
DirectoryInfo.setColumns(20);
DirectoryInfo.setEditable(false);
DirectoryInfo.setForeground(new java.awt.Color(0, 0, 255));
DirectoryInfo.setRows(5);
jScrollPane2.setViewportView(DirectoryInfo);
jLabel5.setText("Active Log :");
jLabel1.setText("List of Shared Files:");
StartServer.setActionCommand("Start ");
StartServer.setFocusCycleRoot(true);
StartServer.setFocusTraversalPolicyProvider(true);
StartServer.setLabel("Start ");
StartServer.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
StartServerActionPerformed(evt);
}
});
StopServer.setActionCommand("End ");
StopServer.setEnabled(false);
StopServer.setLabel("End ");
StopServer.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
StopServerActionPerformed(evt);
}
});
jLabel2.setText("Port Number:");
PortSelected.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
PortSelectedActionPerformed(evt);
}
});
PortSelection.setText("Set Port Number");
PortSelection.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
PortSelectionActionPerformed(evt);
}
});
ActivityLog.setColumns(20);
ActivityLog.setEditable(false);
ActivityLog.setForeground(new java.awt.Color(0, 0, 255));
ActivityLog.setRows(5);
jScrollPane1.setViewportView(ActivityLog);
SetPath.setText("Browse");
SetPath.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SetPathActionPerformed(evt);
}
});
TF_FilePath.setEditable(false);
jLabel3.setText("LocateShraing Folder :");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 694, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(PortSelected, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(PortSelection)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 242, Short.MAX_VALUE)
.addComponent(StartServer, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(48, 48, 48)
.addComponent(StopServer, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(TF_FilePath, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(SetPath)
.addGap(119, 119, 119))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(32, 32, 32)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(70, 70, 70))))
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 674, Short.MAX_VALUE)
.addContainerGap())))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 379, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGap(28, 28, 28)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(PortSelected, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(PortSelection, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(StopServer)
.addComponent(StartServer))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel4)
.addGap(11, 11, 11)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(TF_FilePath, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(SetPath)
.addComponent(jLabel3))
.addGap(29, 29, 29)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void StartServerActionPerformed(java.awt.event.ActionEvent evt) {
if(servPort==0 || TF_FilePath.getText().equals("")) {
JOptionPane.showMessageDialog(null,"Please make sure that you entered a valid port number and a shraing folder path Before Starting a session","WARNING",0);
} else {
preventGUIFreezing();
Runnable readRun = new Runnable() {
public void run() {
PortSelection.setEnabled(false);
SetPath.setEnabled(false);
StartServer.setEnabled(false);
StopServer.setEnabled(true);
ActivityLog.append("Server is Waiting for Clients to connect.\n");
try {
servSocket = new ServerSocket(servPort); //prepare socket
} catch(Exception e) {
JOptionPane.showMessageDialog(null,"Warning:"+e.getMessage(),"WARNING!!",0);
}
while(true) {
try {
sock = servSocket.accept(); //listen for client to connect
if(clientConnectStatus==0) {
remoteClientAddress = sock.getInetAddress().toString().substring(1,(sock.getInetAddress().toString().length()));
ActivityLog.append("Server Connected to "+remoteClientAddress+"\n");
clientConnectStatus = 1;
}
} catch(Exception e) {
JOptionPane.showMessageDialog(null,"Warning:"+e.getMessage(),"WARNING!!",0);
}
//sent the share files name to client
try {
fileDir = new DataOutputStream(sock.getOutputStream());
fileDir.writeUTF(str);
if(fileShareList==0) {
ActivityLog.append("Sent the share file list to client.\n");
fileDir.flush();
fileShareList = 1;
}
} catch(Exception e) {
JOptionPane.showMessageDialog(null,"Warning:"+e.getMessage(),"WARNING!!",0);
}
FileHandle();
}
}
public void FileHandle() {
//receive File name selected by the client
String filename = "";
try {
selectedFile = new DataInputStream(sock.getInputStream());
filename = selectedFile.readUTF();
} catch(Exception e) {
JOptionPane.showMessageDialog(null," Client connected successfully ","NOTIFICATION",1);
//Server Established Connection to the Client for the first time
//FOR the first cycle , selectedFile will be NULL since client hasnt
//request for any GETFILE or PUTFILE command
}
if(filename.equals("")) {
JOptionPane.showMessageDialog(null,"Server is started sharing Files ","Notification",1);
} else {
//if COMMAND = GET
if(filename.substring(0,3).equalsIgnoreCase("get")) {
sentFile(filename.substring(3,filename.length()));
}
//if COMMAND = PUT
if(filename.substring(0,3).equalsIgnoreCase("put")) {
receiveFile(filename.substring(3,filename.length()));
}
}
}
public void receiveFile(String lop) {
try {
Socket sock2 = servSocket.accept();
ActivityLog.append("Start Receiving files ->"+lop+"\n");
int bytesRead2;
int current2 = 0;
long start = System.currentTimeMillis();
//receive file
DataInputStream i2 = new DataInputStream(sock2.getInputStream());
int filesize2 = i2.readInt(); //READING THE FILE SIZE from sender
byte [] mybytearray2 = new byte [filesize2];
InputStream is2 = sock2.getInputStream();
FileOutputStream fos2 = new FileOutputStream(path+"/"+lop);
BufferedOutputStream bos2 = new BufferedOutputStream(fos2);
bytesRead2 = is2.read(mybytearray2,0,mybytearray2.length);
filesize2 = filesize2 - bytesRead2;
current2 = bytesRead2;
do
{
bytesRead2 = is2.read(mybytearray2, current2, (mybytearray2.length-current2));
if(bytesRead2 >= 0) {
current2 += bytesRead2;
}
filesize2 = filesize2 - bytesRead2;
System.out.println(current2);
} while(filesize2>-1);
bos2.write(mybytearray2, 0 , current2);
bos2.flush();
long end = System.currentTimeMillis();
ActivityLog.append("File Transfer will Complete in ->"+(end-start)+"ms.\n");
bos2.close();
sock2.close();
} catch(Exception e) {
JOptionPane.showMessageDialog(null,"Warning:"+e.getMessage(),"WARNING!!",0);
}
}
//string l = filename selected by the client
public void sentFile(String l) {
if(l.equals("")) {
//DoNothing
} else {
ActivityLog.append("Client has selected file to share -> "+l+"\n");
ActivityLog.append("Start Sending File. \n");
try {
//1024 experiment version sending
File myFile = new File(path+"/"+l);
DataOutputStream o = new DataOutputStream(sock.getOutputStream());
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
long lenth=bis.available(); //GET THE INITIAL FILE SIZE
o.writeInt((int)lenth); //SEND TO CLIENT TO TELL THE TOTAL FILE SIZE
byte bytes[] = new byte[bis.available()];
int writecount;
while (lenth > 0) {
if(lenth>=1024) {
writecount = 1024;
} else {
writecount = (int)lenth;
}
bis.read(bytes, 0, writecount);
ActivityLog.append("Bytes Left "+lenth+"\n");
o.write(bytes, 0, writecount);
lenth = lenth - writecount;
}
o.flush();
sock.close();
ActivityLog.append("File Finish Sending. \n");
//1024 experiment version sending
} catch(Exception e) {
JOptionPane.showMessageDialog(null,e.getMessage(),"WARNING!!",0);
}
}
}
};
readThread = new Thread(readRun);
readThread.start();
}//end the FIRST IF statement
}
private void StopServerActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JOptionPane.showMessageDialog(null,"Thank you ");
System.exit(1);
}
private void PortSelectedActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void PortSelectionActionPerformed(java.awt.event.ActionEvent evt) {
try {
int tempPort = Integer.parseInt(PortSelected.getText());
if(tempPort>1023 && tempPort<65535) //start from 1024 - 65534
{
servPort = tempPort;
ActivityLog.append("Port number set To -> "+servPort+"\n");
} else {
JOptionPane.showMessageDialog(null,"Please Enter a valid Port Number, between {1024 - 65534}","Error!!!",0);
}
} catch(Exception e) {
JOptionPane.showMessageDialog(null,"Please Enter Numbers Only, between {1024 - 65534}","Error!!!",0);
}
}
private void SetPathActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int returnVal = chooser.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION) //if the path selected is valid
{
path = chooser.getSelectedFile().getPath(); //set the path into a String
//update The Directory List
File path2 = new File(path);
Filelist = path2.listFiles(); //set filelist = path selected
for(int k=0; k<Filelist.length;k++) //fill up str array with filelist
{
if(Filelist[k].isDirectory()) //if the file is directory
{
continue;
} else {
if(Filelist[k].length()<=902238600) //maximum file size 700mb
{
str = str+Filelist[k].getName()+"\n"; //str will be display in server
}
}
}
if(DirectoryInfo.getText().equals("")) //if directory textArea is empty
{
DirectoryInfo.setText(str); //append the file list
ActivityLog.append("Sharing folder path set to -> "+path+"\n");
TF_FilePath.setText(path);
} else {
DirectoryInfo.setText(""); //clear the directory textArea
DirectoryInfo.setText(str);
ActivityLog.append("Sharing folder path set to -> "+path+"\n");
TF_FilePath.setText(path);
}
}
}
//STOP SERVER CODE
//SET FILE PATH
//START SERVER
//SELECT PORT
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new ServerGUI().setVisible(true);
}
});
}
//method to make sure the threads does not lock up repainting
public void preventGUIFreezing()
{
if(SwingUtilities.isEventDispatchThread())
{
return;
}
throw new RuntimeException("file");
}
// Variables declaration - do not modify
private javax.swing.JTextArea ActivityLog;
private javax.swing.JTextArea DirectoryInfo;
private javax.swing.JTextField PortSelected;
private javax.swing.JButton PortSelection;
private javax.swing.JButton SetPath;
private javax.swing.JButton StartServer;
private javax.swing.JButton StopServer;
private javax.swing.JTextField TF_FilePath;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
// End of variables declaration
}
}