How to upload a file to server?

[nobr]How to upload a file to server?

I tried to do a sample application with the help of

1) http://jakarta.apache.org/commons/fileupload/

2) http://jakarta.apache.org/commons/fileupload/using.html

My code:

1) index.jsp

<html>

<head>

<title>File Upload</title>

<body>

<FORM ENCTYPE='multipart/form-data' method='POST' action='fileUpload.jsp'>

<INPUT TYPE='file' NAME='file1'>

<INPUT TYPE='submit' VALUE='Upload'>

</FORM>

</body>

</html>

2) fileUpload.jsp

<%@ page import="org.apache.commons.fileupload.*"%>

<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>

<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>

<%@ page import="java.util.*"%>

<%@ page import="java.io.File"%>

<html>

<head>

<title>File Upload</title>

<%

// Check that we have a file upload request

boolean isMultipart = ServletFileUpload.isMultipartContent(request);

// Create a factory for disk-based file items

FileItemFactory factory =new DiskFileItemFactory();

// Create a new file upload handler

ServletFileUpload upload =new ServletFileUpload(factory);

// Parse the request

List items = upload.parseRequest(request);

// Process the uploaded items

Iterator iter = items.iterator();

while (iter.hasNext()){

FileItem item = (FileItem) iter.next();

if (!item.isFormField()){

out.println("item.getFieldName() = " + item.getFieldName());%><br><%

out.println("item.getName() = " + item.getName());%><br><%

out.println("item.getString() = " + item.getString());%><br><%

out.println("item.getContentType() = " + item.getContentType());%><br><%

out.println("item.isInMemory() = " + item.isInMemory());%><br><%

out.println("item.getSize() = " + item.getSize());%><br><br><%

File uploadedFile =new File("C:\\Tmp.txt");//Working fine

File uploadedFile =new File("http://www.geocities.com/mysite/");//Not working

item.write(uploadedFile);

}

}

%>

<body>Uploaded Successfully!</body>

</html>

When I try to upload to my system, its working perfectly. My doubt is whether I can upload it directly to an internet site.

Ex:- I need to upload the selected file to my geocity site.

If its possible, pls guide me how to do that in an easiest way

Thanks[/nobr]

[3801 byte] By [astelaveestaa] at [2007-11-26 14:32:49]
# 1
Hello friends. I dont think my question is too complicated. If possible, pls try to help. Cheers
astelaveestaa at 2007-7-8 2:28:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Of course it is NOT possible to write to a foreign server. Imagine everyone could upload files to whichever server they want to.You can only upload the file to the system your application is running on.
JoachimRohdea at 2007-7-8 2:28:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

> Of course it is NOT possible to write to a foreign

> server. Imagine everyone could upload files to

> whichever server they want to.

> You can only upload the file to the system your

> application is running on.

I am not asking abt foreign servers. If I have a webspace, then how can I develop one jsp page which is having an option to upload files to my webspace. Just like geocity site. In geocity you can create ur own free webspace and upload html files to it. Hope its clear now. Cheers.

astelaveestaa at 2007-7-8 2:28:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Your webapplication needs to be installed on the geocity server. And as far as I know, you cannot run JSPs on geocities.
JoachimRohdea at 2007-7-8 2:28:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

> Your webapplication needs to be installed on the

> geocity server. And as far as I know, you cannot run

> JSPs on geocities.

I know very well that my web application needs to be installed on the server. I gave geocity site jus for example. If I am having a webspace supporting java and jsp then how to do that is my question. Understood?

astelaveestaa at 2007-7-8 2:28:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Ah ok. Instead of using absolute paths, try it with relative paths.

So instead of

File uploadedFile = new File("http://www.geocities.com/mysite/");

try

File uploadedFile = new File("/upload-directory");

And make sure, that the 'upload-directory' is existing under your webapplication path.

JoachimRohdea at 2007-7-8 2:28:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...