• Post Reply Bookmark Topic Watch Topic
  • New Topic

help : file transfer problem

 
Jignesh Gohel
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
all. I am making aa application of chat in which there is a module of file transfer when the users are online. I have made the receiving end but I face an exception of corruptstream error. Please help me out I am sending the code with this.
public class Filetransfer extends JPanel implements ActionListener
{
JButton btn;
JFrame fm;
String temp;

File theFile;
File newFile;

URL server;
URLConnection con;

JFileChooser chooser;
ObjectInputStream input;

FileOutputStream outputStream;
FileInputStream inputStream;

public Filetransfer()
{
fm = new JFrame ("Myframe");
btn = new JButton("Send File");

btn.addActionListener(this);

fm.getContentPane().add(BorderLayout.SOUTH,btn);

fm.setSize(300,400);
fm.setVisible(true);
}

public void actionPerformed(ActionEvent ae)
{
String str = ae.getActionCommand();

if(str=="Send File")
{
connection();
}

}
public void connection()
{

try
{
server = new URL("http://localhost:8080/examples/servlet/Fileservlet");
con = server.openConnection();

con.setDoInput(true);
con.setUseCaches(false);

con.setDoOutput(true);
con.setDefaultUseCaches(false);

System.out.println("Successfully connected");

input = new ObjectInputStream(con.getInputStream());

try
{
theFile = (File)input.readObject();
}
catch(Exception e)
{
System.out.println(" File Casting not done " + e);
}

//temp = theFile.toString();
//System.out.println(temp);

//newFile = new File(theFile);
//System.out.println(newFile.getName());

JFileChooser fileSave = new JFileChooser();
fileSave.showSaveDialog(fm);
saveFile(fileSave.getSelectedFile());


}
catch(MalformedURLException me)
{
System.out.println("URL ni Error :....."+me);
}
catch(IOException ioe)
{
System.out.println("Stream ni error:"+ioe);
}

}

public static void main(String args[])
{
new Filetransfer();
}


private void saveFile(File file)
{

try
{

inputStream = new FileInputStream(theFile);

outputStream = new FileOutputStream(file);
outputStream.write(inputStream.read());

outputStream.close();
inputStream.close();

System.out.println("Closed");

}
catch(Exception e)
{
System.out.println("File not found " +e);
}


}



}
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should first familiarize yourself with the Java Documentation. It's an invaulable tool, as attempting to use classes without knowing what they do is pretty futile. Take, for example, java.io.FileInputStream. Look at the read() method which you are using. It doesn't do quite what you expected, does it?
Next, you should take a look at the
Java Tutorial. Tells you the basics on how to use the language, like this trail on Input and Output. It would be invaluable knowledge for someone who wanted to read and write files.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!