wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes File upload Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "File upload" Watch "File upload" New topic
Author

File upload

Raj Puri
Ranch Hand

Joined: Apr 24, 2003
Posts: 189
I have following Java program to copy file to a remote server. Seems it does work but does not copy any file. This should copy from my hard drive to a URL. Any idea?

import java.io.*;
import java.net.*;


public class loadimage {

public static void main(String[] args) {

try
{
URL u = new URL("http://localhost:9050/jsp-examples");
URLConnection uc = u.openConnection();

// uc.connect();
uc.setDoOutput(true);

OutputStream fout = uc.getOutputStream();



FileInputStream fin = new FileInputStream("c:/xxx");

System.out.println("aaa");

byte[] buffer = new byte[256];
while(true)

{

int bytesRead = fin.read(buffer);
System.out.println(bytesRead);
if (bytesRead == -1) break;
fout.write(buffer, 0, bytesRead);
System.out.println("qaaa");

}

fin.close();
fout.close();
}

catch (IOException ioe) {System.err.println(ioe.toString()); }

}
}
Manuel Moons
Ranch Hand

Joined: Mar 05, 2002
Posts: 229
Do you control the server side? Do you read the incoming request and write it somewhere? The problem may be in your server code! What have you got on that end?
Raj Puri
Ranch Hand

Joined: Apr 24, 2003
Posts: 189
Sory for the wrong paste before. Actuall this java program should
copy file xxx to the location as stated in URL along with same file name. It does not work though it compiles and runs. I am trying to get functionality of file upload here.

-------------------------------------------------------------------

import java.io.*;
import java.net.*;


public class loadimage {

public static void main(String[] args) {

try
{
URL u = new URL("http://localhost:9050/jsp-examples/xxx");
URLConnection uc = u.openConnection();

// uc.connect();
uc.setDoOutput(true);

OutputStream fout = uc.getOutputStream();



FileInputStream fin = new FileInputStream("c:/xxx");

System.out.println("aaa");

byte[] buffer = new byte[256];
while(true)

{

int bytesRead = fin.read(buffer);
System.out.println(bytesRead);
if (bytesRead == -1) break;
fout.write(buffer, 0, bytesRead);
System.out.println("qaaa");

}

fin.close();
fout.close();
}

catch (IOException ioe) {System.err.println(ioe.toString()); }

}
}
--------------------------------------------------------------------------------
Manuel Moons
Ranch Hand

Joined: Mar 05, 2002
Posts: 229
Your client code looks OK to me. But I think (if I understand you correctly ) that you do not have a component on the server side that takes in the data that you are sending to it.

In order to upload a file (or send any data to a server) you have to have a Servlet or JSP page that reads the information that you are sending. If you want to save the file on the server, you have to write a Servlet that reads the Http-inputstream and saves it to a location on disk. Do you have such a component?
Raj Puri
Ranch Hand

Joined: Apr 24, 2003
Posts: 189
I thought the following lines will send out put to server and save it to file xxx in jsp_examples folder. I have tried other way round by reading file xxx from jsp_examples and saving it to disk on my PC with swaping input with output that seems to work. Really requirement is to copy a file from client to server of any type(binary)this is all I am trying to do.

URL u = new URL("http://localhost:9050/jsp-examples/xxx");
URLConnection uc = u.openConnection();

// uc.connect();
uc.setDoOutput(true);

OutputStream fout = uc.getOutputStream();
Manuel Moons
Ranch Hand

Joined: Mar 05, 2002
Posts: 229
No, this is not sufficient to write a file at the given location. You will have to write a servlet (or jsp) that will do some processing with the data that you are sending it.

Maybe you should take a look at the Apache Jakarta commons project. (http://jakarta.apache.org/commons/fileupload/)

You have to understand that you can not just write files to a server, if this were so all servers would be overloaded in an instant.

Take a look at the apache commons FileUpload project. You can use it in a servlet that will handle your file uploads.

Read the documentation, look at the examples, everything you need is in there.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File upload