Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training  

uploading a file



Share on Google+Share on Google+

uploading a file
2 Answer(s)      7 years ago
Posted in : JSP-Interview Questions
uploading a file and storing in database using clog and blog?
java code in swing for that i want
View Answers

February 19, 2010 at 11:38 AM

Hi Friend,

Try the following code:

import java.sql.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.filechooser.*;

public class Uploader extends JFrame {
public static JFrame frame;
public JList list;
public DefaultListModel model;
public File currentDir;
public DropTarget target;
JButton addButton, removeButton, uploadButton;
public Uploader() {

Action uploadAction = new UploadAction("Upload");
Action browseAction = new BrowseAction("Browse");
JPanel cp = new JPanel();
cp.setLayout(new BorderLayout());

model = new DefaultListModel();
list = new JList(model);
list.setCellRenderer(new ImageCellRenderer());

target = new DropTarget(list,new FileDropTargetListener());
cp.add(new JScrollPane(list),BorderLayout.CENTER);
JPanel buttons = new JPanel();
buttons.setLayout(new FlowLayout(FlowLayout.RIGHT,5,10));
JLabel uploadLabel = new JLabel();
uploadLabel.setLayout(new FlowLayout(FlowLayout.LEFT,5,10));
uploadButton = new JButton(uploadAction);
addButton = new JButton(browseAction);
currentDir = new File(System.getProperty("user.dir"));
public int addToListModel(ArrayList filenames) {
int count = 0;
for ( Iterator i = filenames.iterator(); i.hasNext(); ) {
String filename = (String);
if (!model.contains(filename)) {
return count;
public static void main(String [] args) {
frame = new Uploader();
class BrowseAction extends AbstractAction {
public BrowseAction(String text) { super(text); }

public void actionPerformed(ActionEvent evt) {
JFileChooser chooser = new JFileChooser(currentDir);
chooser.addChoosableFileFilter(new ImageFileFilter());
currentDir = chooser.getCurrentDirectory();
File [] files = chooser.getSelectedFiles();
ArrayList filenames = new ArrayList(files.length);
for (int i = 0; i < files.length; i++)
class ImageFileFilter extends javax.swing.filechooser.FileFilter {
public boolean accept(File file) {
if (file.isDirectory()) return false;
String name = file.getName().toLowerCase();
return (name.endsWith(".jpg") || name.endsWith(".png")|| name.endsWith(".gif"));
public String getDescription() { return "Images ( *.jpg, *.png,*.gif)"; }

February 19, 2010 at 11:40 AM


class UploadAction extends AbstractAction {
public UploadAction(String text) { super(text); }
public void actionPerformed(ActionEvent evt) {
int selected = list.getSelectedIndex();
Object ob=null;
String fileName=ob.toString();
File file=new File(fileName);
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test";, "root", "root");
PreparedStatement st = con.prepareStatement("insert into file(file,file_data) values(?,?)");
FileInputStream fis = new FileInputStream(file);
st.setBinaryStream(2, (InputStream)fis, (int)(file.length()));
int s = st.executeUpdate();
JOptionPane.showMessageDialog(null,"image is successfully uploaded and inserted.");
catch(Exception ex){}
class ImageCellRenderer extends JLabel implements ListCellRenderer {
public ImageCellRenderer() {
public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus){
File f = new File(value.toString());
Border empty = BorderFactory.createEmptyBorder(3,3,3,3);
Border matte = BorderFactory.createMatteBorder(0,0,1,0,Color.white);
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image icon = toolkit.getImage(f.getPath());
Image scaledIcon = icon.getScaledInstance(40,40,Image.SCALE_FAST);
setIcon(new ImageIcon(scaledIcon));
if (isSelected) {
setBackground(new Color(61,128,223));
} else if (index % 2 == 0) {
setBackground(new Color(237,243,254));
} else {
return this;
class FileDropTargetListener extends DropTargetAdapter {
public void dragEnter(DropTargetDragEvent evt) {
public void drop(DropTargetDropEvent evt) {
try {
Transferable tr = evt.getTransferable();
DataFlavor [] flavors = tr.getTransferDataFlavors();
for ( int i = 0; i < flavors.length; i++ ) {
if ( flavors[i].isFlavorJavaFileListType() ) {
java.util.List list2 = (java.util.List)tr.getTransferData(flavors[i]);
ArrayList filenames = new ArrayList();
for ( int j = 0; j < list2.size(); j++ ) {
String path = list2.get(j).toString();
if ( ((new ImageFileFilter()).accept(new File(path))) ) {
if (filenames.size() > 0 ) {
int numAdded = addToListModel(filenames);
JOptionPane.showMessageDialog(frame,"Error","Invalid File Type",JOptionPane.WARNING_MESSAGE);

} catch (Exception e) {


Related Tutorials/Questions & Answers:
uploading a file
uploading a file  When I am trying to upload a file to another system in lan at a location "",then it is giving the following error message "
uploading audio file iphone
uploading audio file iphone  uploading audio file iphone
file uploading
file uploading  How to upload music files onto the server and save the file information to the mysql database fetch and play the music files on a display page without download thus streaming the files
Uploading File on Server
Uploading File on Server  Hello, Can someone explain or suggest example. How do i uploading files on the FTP Server. Thanks
File Uploading Notification
File Uploading Notification  I am uploading files in my application and i want to know how can i know or be notified when file is uploaded. is there any file uploading event there which can tell me that process is going
uploading file to tomcat server
uploading file to tomcat server  please tell me how to upload a file to the URL "" ? thanks
File uploading - Ajax
File uploading  hi friends, how to uploading the file by using "AJAX".Please send the complete source code for this application   where u want to store the file Can u specify
Uploading a file using UploadBean
Uploading a file using UploadBean  Dear sir, In my project i have to upload the file and use the same file for getting a values from that uploaded .xls file.I used UploadBean for uploading .For the first time when i
query related to uploading file
query related to uploading file  hello friends i am doing my project in servlet and i want to upload a file and store file in local hard drive and file name and path in oracle database.with name file name and path i also want
File uploading - JSP-Servlet
File uploading  i am using file uploading code for multiple file and aslo for single file but i am getting problem that No such file found....
Struts file uploading - Struts
Struts file uploading   Hi all, My application I am uploading... it is breaking while uploading the large file. Please let me know if you know any API... newDocumentForm = (NewDocumentForm) form; FormFile file
Uploading a .3gp file. - MobileApplications
Uploading a .3gp file.  sir, i m sending it again .i want code for java based mobile application, that can take .3GP video file and upload it to youtube using youtube's APIs. The video file would be located on a weblink
uploading a text file into a database
uploading a text file into a database  how to upload a text file... { static File file; public static void main(String[] args) throws Exception { JLabel label=new JLabel("Choose File
File Uploading Problem
File Uploading Problem  I have a file uploading code but it create... = FileUpload.isMultipartContent(request); // Create a new file upload handler... { System.out.println("its a file"); System.out.println
File Uploading Problem
File Uploading Problem  I have a file uploading code but it create... = FileUpload.isMultipartContent(request); // Create a new file upload handler... { System.out.println("its a file"); System.out.println
file uploading using jsp
file uploading using jsp  below file uploading code has one error... = " + formDataLength); //String file = new String(dataBytes); //out.println("FileContents:" + file +""); byte[] line = new byte[128]; if (totalBytesRead
PHP error uploading file - PHP
PHP error uploading file  I am getting error while uploading a file in a folder in PHP ... Warning: Cannot modify header information - headers already send any idea
file Uploading - Development process
file Uploading  Hi all, This is the problem i am facing please help me and solve the problem.i want upload file from my localpc(computer)to any textarea in webapplication.when i drop the mouse on text area the file content
file uploading - Java Beginners
file uploading   hi, i am developing one stand alone j2se programming in that i have to upload a file to another system in the LAN and have... is it should dispaly files from current system and asks whether this file is already
Uploading and download pdf or .txt file.
Uploading and download pdf or .txt file.  I want admin user to upload pdf file into database and the users can download those pdf format from database
file uploading - JSP-Servlet
file uploading  Hi, thanks a lot for your kind answer. Now my program... problem. Im not geeting the full output for the program. Even, the file... to solve the problem as well. Thanks in advance. Input File
FILE UPLOADING  Hi , I want Simple program for file upload using html and servlet plese help me  hi friend pls try this code ********** try{ String type=""; String boundary=""; String sz
file uploading - JavaMail
file uploading  Hi thi is swathi.Thank s for giving the answers to previous questions.I am getting the problem with below code FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new
Drag and drop file uploading - Ajax
Drag and drop file uploading  Hi all, This is NageswaraRao i want file uploading feature on my web development..using drag and drop mouse functionality. Problem:I have Created one Text area when i drop the file on text area
Uploading a single file by using JSp
Uploading a single file by using JSp  u have said about submit button..but in program u have not used submit button..and where file will be stored..where should we specify the output folder name..   Visit Here
Uploading tha file - JSP-Servlet
Uploading tha file  hi,Sir. this is siddaiah. please give me response for the follwing questions. to upload file into oracle database using JSP? 2.How to upload photo into oracle database using JSP? Regards
Uploading the file on Server.
Uploading the file on Server This example will explain you how to upload the file on FTP server. ftp_put() command allows to upload an existing file on the server. For uploading a file onto the FTP server, first you have to login
What changes I have to do in php.ini file for file uploading?
What changes I have to do in php.ini file for file uploading?  What changes I have to do in php.ini file for file uploading
uploading a file - JSP-Interview Questions
uploading a file   uploading a file and storing in database using...; public File currentDir; public DropTarget target; JButton addButton...,BorderLayout.SOUTH); currentDir = new File(System.getProperty("user.dir")); } public
Uploading a single file by using JSp
Uploading a single file by using JSp  u have said about submit button..but in program u have not used submit button..and where file will be stored...;b>Choose the file To Upload:</b></td> <td><INPUT NAME
Multiple file Uploading - JSP-Servlet
Multiple file Uploading   Hello everyone I am using jsp and my IDE... class for JSP: An error occurred at line: 9 in the generated java file... file Only a type can be imported