Está en la página 1de 4

import java.io.

BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.net.Socket;
import java.net.UnknownHostException;
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 programterima extends javax.swing.JFrame {

/**
* Creates new form programterima
*/
public programterima() {
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() {

simpan = 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);

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

close.setText("close");

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(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(simpan)
.addGap(18, 18, 18)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 255,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(188, 188, 188)
.addComponent(close)))
.addContainerGap(74, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(58, 58, 58)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(simpan)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 31,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(30, 30, 30)
.addComponent(close)
.addContainerGap(55, Short.MAX_VALUE))
);

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

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

JFileChooser pilih = new JFileChooser();


pilih.setDialogTitle("Tentukan tempat penyimpanan");
int userSelection = pilih.showSaveDialog(null);
if (userSelection == JFileChooser.APPROVE_OPTION){
File fileToSave = pilih.getSelectedFile();
System.out.println("Save as file:" + fileToSave.getAbsolutePath());
txtPath.append(fileToSave.getAbsolutePath());
try {
int buffer_Size=1024;
byte[] buffer = new byte[buffer_Size];
Socket socket = new Socket("127.0.0.1", 1214);
BufferedInputStream in = new
BufferedInputStream(socket.getInputStream());
BufferedOutputStream out = new BufferedOutputStream(new
FileOutputStream(fileToSave.getAbsolutePath()));
int len;
while ((len= in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.flush();
}
catch (UnknownHostException ex) {
//Logger.getLogger(programterima.class.getName()).log(Level.SEVERE,
nul, ex);

} catch (Exception e) {
System.out.println("ada error file cek lagi");
}
}
}
/**
* @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(programterima.class.getName()).log(java.util.log
ging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {

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

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

java.util.logging.Logger.getLogger(programterima.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 programterima().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton close;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton simpan;
private javax.swing.JTextArea txtPath;
// End of variables declaration
}

También podría gustarte