You are on page 1of 4

import java.io.

BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Path;
import javax.swing.JFileChooser;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author LENOVO PC
*/
public class programkirim2 extends javax.swing.JFrame {

/**
* Creates new form programkirim2
*/
public programkirim2() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">

private void initComponents() {

sendfile = new javax.swing.JButton();


pilihfile = new javax.swing.JButton();
close = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
txtpath = new javax.swing.JTextArea();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

sendfile.setText("sendfile");
sendfile.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sendfileActionPerformed(evt);
}
});

pilihfile.setText("pilihfile");
pilihfile.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
pilihfileActionPerformed(evt);
}
});

close.setText("close");
close.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
closeActionPerformed(evt);
}
});

txtpath.setColumns(20);
txtpath.setRows(5);
jScrollPane1.setViewportView(txtpath);

javax.swing.GroupLayout layout = new


javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addGap(52, 52, 52)
.addComponent(pilihfile)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30,
Short.MAX_VALUE)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 284,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(sendfile)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(close)))
.addGap(69, 69, 69))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap(35, Short.MAX_VALUE)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(pilihfile)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 42,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(46, 46, 46)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(sendfile, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(close, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(50, 50, 50))
);

pack();
}// </editor-fold>

private void pilihfileActionPerformed(java.awt.event.ActionEvent evt) {

JFileChooser pilih=new JFileChooser();


int confirm=pilih.showOpenDialog(null);
if(confirm==JFileChooser.APPROVE_OPTION){
Path path=pilih.getSelectedFile().toPath();
txtpath.append(path.toString());
}// TODO add your handling code here:
}

private void sendfileActionPerformed(java.awt.event.ActionEvent evt) {

try {
int buffer_Size=1024;
byte[] buffer = new byte[buffer_Size];
ServerSocket servsock = new ServerSocket(1214);
Socket sock = servsock.accept();
System.out.println("koneksi terbentuk");
String folderFile=txtpath.getText();

BufferedInputStream in = new BufferedInputStream(new


FileInputStream(folderFile));
BufferedOutputStream out = new
BufferedOutputStream(sock.getOutputStream());

int len;
while ((len = in.read(buffer)) > 0){
out.write(buffer,0,len);
}
out.flush();
servsock.close();
} catch (IOException ex){
// Logger.getLogger(programkirim2.class.getName()).log(Level.SEVERE,
null, ex);
}
}

private void closeActionPerformed(java.awt.event.ActionEvent evt) {

System.exit(0);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code
(optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the
default look and feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(programkirim2.class.getName()).log(java.util.log
ging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(programkirim2.class.getName()).log(java.util.log
ging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(programkirim2.class.getName()).log(java.util.log
ging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(programkirim2.class.getName()).log(java.util.log
ging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new programkirim2().setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.JButton close;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton pilihfile;
private javax.swing.JButton sendfile;
private javax.swing.JTextArea txtpath;
// End of variables declaration
}

You might also like