import java.io.File;
public class FileHelper {
private static File f;
public static final String root="upload";
static{
f=new File(root);
if(!f.exists())f.mkdir();
}
public static String getProfileLocation(){
File profile=new File(f+File.separator+"profile");
if(!profile.exists())profile.mkdir();
return profile.getAbsolutePath();
}
public static String getAssignLocation(){
File profile=new File(f+File.separator+"assign");
if(!profile.exists())profile.mkdir();
return profile.getAbsolutePath();
}
public static String getNoteLocation(){
File profile=new File(f+File.separator+"note");
if(!profile.exists())profile.mkdir();
return profile.getAbsolutePath();
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
/*
* FileUpload.java
*
* Created on __DATE__, __TIME__
*/
/**
*
* @author __USER__
*/
public class FileUpload extends javax.swing.JFrame {
/** Creates new form FileUpload */
public FileUpload() {
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.
*/
//GEN-BEGIN:initComponents
//
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jComboBox1 = new javax.swing.JComboBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Browse");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Upload Image");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jLabel1.setText("Your Image Location ");
jTextField1.setEditable(false);
jTextField1.setText(" ");
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
"Select Type", "Profile", "Assign", "Note" }));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout
.setHorizontalGroup(layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createSequentialGroup()
.addGap(29, 29, 29)
.addComponent(jLabel1)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(
jTextField1,
javax.swing.GroupLayout.DEFAULT_SIZE,
322, Short.MAX_VALUE)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1).addGap(40, 40,
40)).addGroup(
layout.createSequentialGroup().addGap(213, 213,
213).addComponent(jButton2)
.addContainerGap(279, Short.MAX_VALUE))
.addGroup(
layout.createSequentialGroup().addGap(183, 183,
183).addComponent(jComboBox1,
javax.swing.GroupLayout.PREFERRED_SIZE,
276,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(140, Short.MAX_VALUE)));
layout
.setVerticalGroup(layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createSequentialGroup()
.addGap(44, 44, 44)
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jButton1)
.addComponent(
jTextField1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(119, 119, 119)
.addComponent(
jComboBox1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED,
182, Short.MAX_VALUE)
.addComponent(jButton2).addGap(35, 35,
35)));
pack();
}//
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
if (!jTextField1.getText().equals("")&& !jComboBox1.getSelectedItem().toString().equalsIgnoreCase("select type")) {
File ff=new File(jTextField1.getText());
try {
String type=jComboBox1.getSelectedItem().toString();
String destination="";
if(type.equalsIgnoreCase("profile")){
destination=FileHelper.getProfileLocation()+File.separator+ff.getName();
}else if(type.equalsIgnoreCase("assign")){
destination=FileHelper.getAssignLocation()+File.separator+ff.getName();
}else if(type.equalsIgnoreCase("note")){
destination=FileHelper.getNoteLocation()+File.separator+ff.getName();
}else{
JOptionPane.showMessageDialog(this, "Sorry !");
}
FileInputStream fi = new FileInputStream(ff.getAbsolutePath());
FileOutputStream fo=new FileOutputStream(destination);
int i = 0;
while ((i = fi.read()) != -1) {
fo.write(i);
fo.flush();
}
//database code insert method
System.out.println(destination);
fi.close();
fo.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser c = new JFileChooser();
c.setFileFilter(new FileFilter() {
@Override
public String getDescription() {
// TODO Auto-generated method stub
return "Image File";
}
@Override
public boolean accept(File f) {
// TODO Auto-generated method stub
return f.getName().endsWith(".png");
}
});
int i = c.showOpenDialog(this);
if (i == JFileChooser.APPROVE_OPTION) {
File f = c.getSelectedFile();
String path = f.getAbsolutePath();
jTextField1.setText(path);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FileUpload().setVisible(true);
}
});
}
//GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField jTextField1;
// End of variables declaration//GEN-END:variables
}
No comments:
Post a Comment